[Pyrex] access to c data class members

ahalda at cs.mcgill.ca ahalda at cs.mcgill.ca
Mon Aug 28 20:11:11 UTC 2006


OK, things are clearer! 'newObj = MyClass()' is implicitly creating a
generic python object.

Your superclass analogy idea makes sense.
If I just remember the implicit code, or write it out explicitly, it's
easy to see what's going on.

Anyway, my original problem was realizing that after doing
'cdef MyClass newObj' I also had to do 'newObj = MyClass()'.
It seems obvious now, I don't know what I was thinking. Maybe
the fact that is is not totally c-like, using 'constructor'
syntax.

Thank you for your time, both of you. Your comments were very helpful.
My pyrex code works great, I just need to learn python properly now.

Allan


On Mon, August 28, 2006 1:23 pm, Daniele Varrazzo wrote:
>> 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