Checking the type of a PyObject

wim delvaux wim.delvaux at adaptiveplanet.com
Sun Aug 12 13:06:03 EDT 2001


Alex Martelli wrote:

> Hello Wim,
>
> > Question remains.  How can I get access to the PyString_Type variable
> > (buffer) declared in the extension module (if I remember)
> > for those abstract objects, if you do not know the name of the buffer
> > but only the name of the type.
>
> No general way -- the extension module needs to supply a suitable .h
> and/or some other C API.  Normally, extension modules make everything
> static (visible only within their own .C source files), so that may not be
> directly possible.
>
> Presumably however there is a work-around.  Doesn't the extension
> module expose (to Python) some attribute which is this type object
> that you seek?  You could get at it from C.  Or else, can you somehow
> get hold of an object, ANY object, of that type you're interested in
> (somehow exposed by the extension module, or maybe the module
> exposes a factory function that you can call to get a sample object
> once at the start of your own code)?  If so, PyObject_Type(...) on
> that sample object will get you the type-object you seek.
>
> Incidentally, PyObject_IsInstance(PyObject* obj, PyObject* type)
> is generally a better way to typecheck than checking for type objects
> identity.  In Python 2.1 it's basically the same, but in 2.2 and later,
> when inheritance becomes possible from builtin-types and extension
> types, checking with isinstance is almost always better (any subclass
> will satisfy that test, rather than just the base-type).

    Where does that PyObject_isInstance get the second argument from.  Is
that again that PyString_Type pointer or somewhere else ?

    W

>
>
> Alex
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com




More information about the Python-list mailing list