<font color="#000000">Hello everyone,<br><br>This is probably a basic question with an obvious answer, but I don't quite get why the type(foo).__name__ works differently for some class instances and not for others.  If I have an "underived" class, any instance of that class is simply of type "instance".  If I include an explicit base class, then its type __name__ is the name of the class.<br>
<br>$ python<br>Python 2.7.2 (default, Aug 26 2011, 22:35:24) <br>[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin<br>Type "help", "copyright", "credits" or "license" for more information.<br>
>>> class MyClass:<br>...     pass<br>... <br>>>> foo = MyClass()<br>>>> type(foo)<br><type 'instance'><br>>>> type(foo).__name__<br>'instance'<br>>>> class MyClass1():<br>
...     pass<br>... <br>>>> bar = MyClass1()<br>>>> type(bar)<br><type 'instance'><br>>>> type(bar).__name__<br>'instance'<br>>>> class MyClass2(object):<br>...     pass<br>
... <br>>>> foobar = MyClass2()<br>>>> type(foobar)<br><class '__main__.MyClass2'><br>>>> type(foobar).__name__<br>'MyClass2'<br><br>I can't explain this behavior (since doesn't every class inherit from object by default? And if so, there should be no difference between any of my class definitions).  I would prefer that every approach give me the name of the class (rather than the first 2 just return 'instance').  Why is this not the case?  Also, is there any way to access the name of the of the class type foo or bar in the above example?<br>
<br>Thanks!<br>Jason<br><br>P.S.  I'll note that my "preferred" behavior is how python3.2 actually operates<br><br>$ python3.2<br>Python 3.2.1 (default, Aug 26 2011, 23:20:19) <br>[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin<br>
Type "help", "copyright", "credits" or "license" for more information.<br>>>> class MyClass:<br>...     pass<br>... <br>>>> foo = MyClass()<br>>>> type(foo).__name__<br>
'MyClass'<br><br><br></font>-- <br>Jason M. Swails<br>Quantum Theory Project,<br>University of Florida<br>Ph.D. Candidate<br>352-392-4032<br>