using the C API to access objects derived from builtin classes
Stefan Seefeld
seefeld at sympatico.ca
Wed May 19 10:11:30 EDT 2004
Thomas Heller wrote:
>>But back to the other issue: isn't there any text that explains
>>when I should be using PyDict_GetItem and when PyObject_GetItem
>>and what they are doing byhind the scene ?
> 7. Concrete Objects Layer
>
> The functions in this chapter are specific to certain Python object
> types. Passing them an object of the wrong type is not a good idea; if
> you receive an object from a Python program and you are not sure that
> it has the right type, you must perform a type check first; for
> example, to check that an object is a dictionary, use
> PyDict_Check(). The chapter is structured like the ``family tree'' of
> Python object types.
yeah, I did read that. The inconsistency (or even error, I would argue) is
in the interpretation of 'is a'. PyDict_Check reports 'true' when called
on my 'Dictionary' object, yet the PyDict_* API only applies to the
slice representing the base class. I don't think that is correct, it is
at least very surprising.
The API only supports two extremes:
* the object's type is an *exact* match for a built-in type, thus a highly
optimized API can be used to access its methods.
* the object's type is unknown, it may or may not support a certain 'protocol',
i.e. for all method invocations a full method lookup has to be done.
That sounds good for the pre 2.2 world, where built-in types and user-defined
types were distinct sets.
Regards,
Stefan
More information about the Python-list
mailing list