Re: [Cython] [Pyrex] exposing extension class attributes

Robert Bradshaw <[email protected]>
Newsgroups gmane.comp.python.cython.devel,gmane.comp.python.pyrex
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.