Type of an object: ‘obj.__class__’ versus ‘type(obj)’

Ben Finney ben+python at benfinney.id.au
Mon Dec 16 02:51:21 CET 2013


Howdy all,

What is the Pythonic way to determine the type of an object? Are there
multiple valid ways, and when should each be used?

We have ‘obj.__class__’, an attribute bound to the object's class. Or is
it? When is that true, and when should we not rely on it?

We have ‘type(obj)’, calling the constructor for the ‘type’ type in
order to get a reference to the type of ‘obj’. Or is it? When is that
true, and when should we not rely on it?

Are there other ways to get at the type of a Python object? What reasons
are there to choose or avoid them?

-- 
 \         “Pinky, are you pondering what I'm pondering?” “I think so, |
  `\    Brain, but Zero Mostel times anything will still give you Zero |
_o__)                                  Mostel.” —_Pinky and The Brain_ |
Ben Finney




More information about the Python-list mailing list