[Cython] Weirdness with Cython class: "object has no attribute ..."
Stefan Behnel
stefan_ml at behnel.de
Sat May 16 20:33:31 CEST 2009
Juha Salo wrote:
> I'm totally out of ideas with this one. I have a Cython class defined in
> .pyx and .pxd files like this:
>
> #foo.pxd
>
> cdef class Foo:
> cdef float n[3]
>
> #foo.pyx
>
> cdef class Foo:
> def __init__(self, float a=0.0, float b=0.0, float c=0.0):
> self.n[0] = a
> self.n[1] = b
> self.n[2] = c
>
>
>
> The problem is that when I try to import and initialize a Foo object, I get
> the following error:
>
> >>> from foo import Foo
> >>> f = Foo()
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "foo.pyx", line 3, in foo.Foo.__init__ (foo.c:701)
> self.n[0] = a
> AttributeError: 'foo.Foo' object has no attribute 'n'
>
>
>
> I've tried all kinds of crazy stuff trying to fix this and I found a strange
> thing: When I copy foo.pxd and foo.pyx to another folder and build them from
> that path, they work and I get no errors at all. What could cause something
> like this?
You must keep both in a "foo" package directory that has an __init__.py
file (to mark it as package). That's the way Python packages work.
Stefan
More information about the Cython-dev
mailing list