[Pyrex] exposing extension class attributes
Robert Bradshaw
robertwb at math.washington.edu
Tue Mar 18 23:41:45 CET 2008
On Mar 18, 2008, at 10:55 AM, Simon Burton wrote:
>
> I am considering using some kind of name convention to distinguish
> between
> extension class access at the python and at the c level:
>
> cdef class Foo:
>
> cdef int _foo
>
> def __init__(self, int foo):
> self._foo = foo
>
> property foo:
> def __get__(self):
> return self._foo
>
>
> Ie. the c attribute has an underscore in front of it, and
> from python the attribute is without underscore.
>
> As this is likely to drive me moderately crazy (keeping track
> of underscores) I am wondering how other people handle this issue ?
Declare foo as public or readonly (e.g. cdef public int foo or cdef
readonly int foo). Pyrex/Cython will then create the get/set property
methods for you.
> I guess I am just trying to write C code without writing C code (or
> C++ code).
> Perhaps I can abandon (exposing these classes to) the python
> interpreter altogether,
> and stick to writing pyrex code.
>
> A similar problem occurs with methods. I gave up on duplicating
> every method
> (a cdef version and a python version), and am just using python
> methods for now.
Cython has "cpdef" methods that does this for you--if you call it
from C it's a cdef method, if you call it from Python it's a def
method. These can be overridden in the usual way and it does all the
right stuff.
- Robert
More information about the Pyrex
mailing list