[Pyrex] access to c data class members

Daniele Varrazzo daniele.varrazzo at gmail.com
Mon Aug 28 17:23:25 UTC 2006


> What confused me is the c and python syntax are overlain.
> That section only shows how to use 'int' data types, which are
> automatically  converted for us. However I am creating python objects.
>
> A line
>    newObj = MyClass()
> makes sense on its own. It is like in python, where we don't declare type.
> But when doing
>   cdef MyClass newObj
>   newObj = myClass()
> we are defining a c-style object. (Also, since classes don't exist in C,
> this feels funny. but its ok for cpp)

Maybe you can have some light shed if you think that, in your first
example, there is an implicit

    cdef object newObj

which is added by the Pyrex compiler. So you really have to compare
the two cases:

a)
    cdef object newObj
    newObj = MyClass()

b)
    cdef MyClass newObj
    newObj = MyClass()

The first line in both snippets is mapped to a C declaration:
something similar to
 a) PyObject *newObj;
 b) MyClass *newObj;

You can view "object" as MyClass "superclass" (as in C++), so both the
assignment in the second lines can work. (it naturally works in C++:
Pyrex compiler implements it in C with some casting work)

In both examples you can access to any Python attribute of newObj,
because they are dynamically looked up and the lookup mechanism is in
in "object" (and is "inherited" by MyClass). But if you want to access
to C attributes defined on MyClass you need newObj to be a MyClass
pointer. They are not in the Python dictionary but in a C structure,
so the lookup mechanism can't reach them.



More information about the Pyrex mailing list