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

Ben Finney ben+python at benfinney.id.au
Sun Dec 15 23:50:17 EST 2013


Ned Batchelder <ned at nedbatchelder.com> writes:

> Generally, my answer would be, "You probably don't need the type as
> much as you think you do."  
> […]
> Also, don't overlook isinstance().

Agreed.

> But when you do need it, type(x) is better than x.__class__, simply
> because we should always favor builtin functions over direct access of
> dunder-names where possible.

Thanks.

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?

-- 
 \       “For fast acting relief, try slowing down.” —Jane Wagner, via |
  `\                                                       Lily Tomlin |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list