new-style class instance check

Michael Hudson mwh at python.net
Mon Apr 12 06:08:30 EDT 2004


"Richard Gruet" <rjgruet at yahoo.com> writes:

> Right, you are perfectly right.
> As Robert (Brewer) guessed, I was actually naively trying to detect
> whether an object was an instance of a *user-defined* new-style
> class in an attempt to find and equivalent to type(o) is
> InstanceType for new-style classes - so my function could be more
> appropriately named e;G.  isUserDefinedNewStyleClassInstance(o).

You could check the tp_flags member for the TP_HEAPTYPE bit -- but
this is C talk and doesn't translate into Python that cleanly
(o.__class__.__flags__ & (1<<9) or similar) and isn't 100%
reliable. Of course, type(o) is InstanceType isn't reliable either --
it's true for e.g. Exception.

Why do you want to know?

Cheers,
mwh

-- 
  Windows XP: Big cow. Stands there, not especially malevolent
  but constantly crapping on your carpet. Eventually you have to
  open a window to let the crap out or you die.
                           -- Jim's pedigree of operating systems, asr



More information about the Python-list mailing list