How does the type
function, when passed a single argument, determines the type of the object it is passed?
And is it possible to customize what it returns by making some particular changes in the object's class
definition?
Edit:
I doubt it uses the object.__class__
attribute because in spite of overriding the __getattribute__
function of the class to return some arbitrary value when querying for __class__
it returns the actual type when the type checked through type
.
>>> class Foo(object):
... def __getattribute__(self, name):
... if name == "__class__":
... return type("Bar", (), {})
... else:
... return super(Foo, self).__getattribute__(name)
...
>>> bar = Foo()
>>> bar.__class__ , type(bar)
(<class '__main__.Bar'>, <class '__main__.Foo'>)