Python: self.__class__ vs. type(self) [duplicate]

>>> class Test(object): pass
>>> t = Test()
>>> type(t) is t.__class__
True
>>> type(t)
__main__.Test

So those two are the same. I would use self.__class__ since it’s more obvious what it is.

However, type(t) won’t work for old-style classes since the type of an instance of an old-style class is instance while the type of a new-style class instance is its class:

>>> class Test(): pass
>>> t = Test()
>>> type(t) is t.__class__
False
>>> type(t)
instance

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)