Hi,

Jørgen P. Tjernø wrote:
> I'm currently using Cython to provide some bindings to a C api. I'm 
> using classes to "wrap" the various C structs, but they're VERY 
> repetetive to write, since many of the attribute I expose are in the form:
> 
> property title:
>      def __get__(self):
>          return self.track.title
> 
> Is there any shorthand for this? :-)

No. We could potentially transform

     @property
     def title(self): return self.track.title

into the above code, but that wouldn't be that much shorter either.

http://trac.cython.org/cython_trac/ticket/264

Note that there is a shortcut for direct class attributes, though, so you
could write

    class SomeClass:
        cdef readonly object some_attribute

to get a read-only property "some_attribute" that is still accessed
efficiently (and is writable) from Cython code. But this does not work for
your case above.


> Also, I want to initialize them with a C struct pointer (they're only 
> ever initialized by my Cython code, not by the user themselves), is 
> there a better way than:
> 
> cdef class Artist:
>      def __init__(self):
>          raise TypeError("This class cannot be instantiated from Python")
> 
> cdef extern from "foo.h":
>      cdef Artist NEW_ARTIST "PY_NEW" (object t)
> 
> # #define PY_NEW(T) \
> #     (((PyTypeObject*)(T))->tp_new( \
> #             (PyTypeObject*)(T), __pyx_empty_tuple, NULL))
> 
> cdef Artist _create_artist(artist* artist, bool take_owner=False):
>      cdef Artist instance
> 
>      instance = NEW_TRACK(Artist)
>      instance.artist = artist
>      instance.take_owner = take_owner
>      return instance

Again, we could potentially do some things here.

http://trac.cython.org/cython_trac/ticket/263

However, this is a pretty common and also very fast way of doing this, so
you can't currently do better.

Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to