[IronPython] How to get the Type of object

Curt Hagenlocher curt at hagenlocher.org
Thu Sep 9 02:12:50 CEST 2004

"senthilkumar" <senthilkumar.chockalingam at kla-tencor.com> writes:

> I would like to know is there any way to get the type of the object.

There are two types associated with every object in IronPython.
The first is its Python type, which can be gotten using the
type() operator, and the second is its CLR type, which can be
gotten using GetType().

Consider the following code:

import System
from System.Text import StringBuilder
class MyStringBuilder(object):
sb = StringBuilder()
msb = MyStringBuilder()
print type(sb), type(msb)
print sb.GetType(), msb.GetType()

The first statement will print 
<type 'System.Text.StringBuilder'> <class '__main__.MyStringBuilder'>
and the second will print
System.Text.StringBuilder IronPython.NewTypes.System.Object

More importantly, if I defined another class as 

class OtherStringBuilder(object):

then type(MyStringBuilder()) != type(OtherStringBuilder()), but for
now, MyStringBuilder().GetType() == OtherStringBuilder.GetType()
because each is based on the same underlying CLR type.

Curt Hagenlocher
curt at hagenlocher.org

More information about the Ironpython-users mailing list