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

Devin Jeanpierre jeanpierreda at gmail.com
Mon Dec 16 00:56:26 EST 2013


On Sun, Dec 15, 2013 at 8:50 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> Should we expect (ignoring pathological cases) the assertion
> ‘type(obj) is obj.__class__’ to hold true? If not, under what
> circumstances would it be sensible for those to differ?

They differ on old-style classes (in 2.x): the type will be either
classobj or instance; and the __class__ doesn't exist (for class
objects) or is what you'd expect (for instances).

This isn't particularly pathological, so I disagree with the
suggestion that one should always definitely use type() for style
reasons.

-- Devin



More information about the Python-list mailing list