[Pyrex] access to c data class members

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


(Sorry Greg, I sent this personally to you by accident)

On Sun, August 27, 2006 10:12 pm, Greg Ewing wrote:
> ahalda at cs.mcgill.ca wrote:
>> So, the solution is to do BOTH strategies at once, on the same name:
>> Define it a C-object, and then *also* as a python object.
>>
> You seem confused. It's not a matter of declaring it as
> two different things at once. You declare the type of the variable, and
> then assign a value to it.

OK. I see that now. (I think). But I feel I would have to dig deeper
into the workings of pyrex to really understand it.

>> Maybe that should go in the language definition page in the 'tricks'
>> section. It is a little non-intuitive
>
> There's nothing tricky about it; it's just a direct
> consequence of Pyrex's type system. The section on "Attributes" under
> "Extension Types" mentions the
> need for such declarations, although admittedly it's rather compressed and
> would be easy to miss on a first reading. I'll think about expanding it a
> bit.

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)

It looked to me like the same thing, but they are different.

Maybe I still have it wrong, but at any rate I can use it now, without
having to understand all the cogs in the background (which is part of
the point of pyrex, right?). Maybe I'm the only one with this problem, but
I think an explicit example in the docs using objects as opposed to ints
would have helped me.

I also have to admit that I have only started learning python a
few days ago.. and I know very little about the c/python api.
Besides this confusion, pyrex was straightforward even for me.

Allan




More information about the Pyrex mailing list