[Pyrex] FAQ contribution
John J Lee
jjl at pobox.com
Mon Jul 21 19:52:57 CEST 2003
Despite the clarity of the docs, I managed to trip over this about four
times, and it took me a while to figure it out each time :-(don't ask me
why I didn't learn the *third* time...).
Worth a FAQ?
Q. Pyrex says my extension type object has no attribute 'rhubarb', but
I know it does.
A. Have you declared the type at the point where you're using it?
cdef class ExtClass1
cdef MyCType rhubarb
...
cdef class ExtClass2:
cdef object mylist
def __init__(self):
self.mylist = []
def append(self, ExtClass1 e):
self.mylist.append(e)
def foo(self):
for e in self.mylist:
do_something(e.rhubarb) # WRONG
def bar(self, e):
do_something(e.rhubarb) # WRONG
Pyrex will say (at runtime):
...
AttributeError: 'mymodule.ExtClass1' object has no attribute 'rhubarb'
Pyrex didn't know at compile time that e is an instance of the extension
class ExtClass1 (which is needed to do a direct C lookup). You can't make
rhubarb a public attribute either (which is needed to do a dynamic Python
attribute lookup), because it has a plain C type, not an extension type.
Here's how to tell Pyrex the type at compile time:
def foo(self):
cdef ExtClass1 e
for e in self.mylist:
do_something(e.rhubarb)
def bar(self, ExtClass1 e):
do_something(e.rhubarb)
John
More information about the Pyrex
mailing list