C API PyObject_GetAttrString returns <getset_descriptor> not the object I expected

Stefan Behnel stefan_ml at behnel.de
Sun Feb 10 08:58:57 EST 2019


Barry Scott schrieb am 10.02.19 um 13:08:
> After calling PyObject_GetAttrString() I expected to get a PyObject string 
> back but I found that I had been given a <getset_description> instead.
> 
> (gdb) p *args_o 
> $4 = <getset_descriptor at remote 0x7fffea87c7e0>
> 
> What is going on and how do I get from the <getset_descriptor> to the object I 
> want?

Phil is right about the function itself, but my guess is that you called
GetAttr() on a class instead of an instance. Read up on Python descriptors
to understand what difference that makes.

https://docs.python.org/3/howto/descriptor.html

Basically, you got something like a property object back, but not the value
that the property maintaines. If you look up the attribute on the instance,
the property (or descriptor) will hand it to you. The same applies to
method lookups and other special attributes that may also be implemented as
descriptors.

Also take a look at Cython, which is designed to keep users from having to
learn all these things and instead lets you do them in Python.

https://cython.org/

Stefan




More information about the Python-list mailing list