I have the following code:
>>> class MyClass:
pass
>>> myObj=MyClass()
>>> type(myObj)
<type 'instance'> <==== Why it is not type MyClass ?
>>> type(MyClass)
<type 'classobj'> <=== Why it is not just 'MyClass'?
>>> isinstance(myObj, instance) <==== Why the 'instance' is not defined?
Traceback (most recent call last):
File "<pyshell#91>", line 1, in <module>
isinstance(myObj, instance)
NameError: name 'instance' is not defined
>>> isinstance(myObj, MyClass)
True
>>> myObj.__class__
<class __main__.MyClass at 0x0000000002A44D68> <=== Why different from type(myObj) ?
It seems Python has some extra indirection between a class and its instance type.
I am used to C#. In C#, typeof(MyClass)
will just return the MyClass
.
Add 1
Below is some comparison between 2.7.6 and 3.4.1.
I am wondering how the ==
operator is implemented in Python.