[Python-Dev] PyInstance_Check() and new-style classes
ewilhelm at sbcglobal.net
Mon Jul 12 16:01:08 CEST 2004
# The following was supposedly scribed by
# Michael Hudson
# on Monday 12 July 2004 08:08 am:
>> tell me if I'm doing this "the right way" (TM).
>Why does it matter? I did actually read the rest of your post, but
>this failed to leap out at me (apologies if I'm being dumb).
For starters, the process-of-elimination way of determining the type is pretty
clunky, and only works on classes which have subclassed the 'object' type
(not lists, tuples, strings, etc.)
Also, it seems that the type/class unification has broken the API function
PyInstance_Check(), which I think should tell me if I'm dealing with an
instance of a builtin type. If not, it seems that there should be some
function which allows me to perform this check.
>There's no real answer -- there's just not that much difference
>between user-defined new-style classes and builtin types (part of
>their appeal!) but checking the TP_HEAPTYPE flag in tp_flags may go
>some way towards one.
I'll have to look into this. I'm not sure what that would tell me.
Basically, Perl's "objects" are just bless()ed references. That seems
analogous to the direction that Python has gone with the type/class
unification, but Perl is a little less formal about objects.
This brings up another problem with my current code in that a subclass of an
actual builtin type (as opposed to just 'object') needs to be handled
differently when constructing the perl values. e.g. I'd like to return a
blessed reference to a Perl array if the class is subclassing the python
array. This won't work with the current scheme, since the blessed reference
for a class instance is a reference to a scalar containing its memory
However, I'm willing to punt that and just use the pointer to the python
object (be it a list, tuple, or object) provided that I can identify it as a
But, this won't work with the way things are now, since PySequence_Check()
would presumably return true for an instance of a class which had subclassed
the builtin list type.
Granted, we could just pass pointers to python objects for everything, but
that wouldn't make a very good perl binding, since we'd then have to use
$string->append("foo") instead of Perl's builtin $string .= "foo" and I'm not
sure yet how we would get to the actual value of $string under that schema.
"Because understanding simplicity is complicated."
More information about the Python-Dev