[Cython] Cython methods for C/C++ types

mark florisson markflorisson88 at gmail.com
Tue Jul 3 12:12:21 CEST 2012


On 3 July 2012 05:58, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Hi,
>
> the discussion on allowing for automatic user provided ways to coerce
> between Python types and C++ types got me thinking that this might hide a
> rather interesting general feature: methods for low-level types. I faintly
> remember that this idea has come up in our discussions before, but here's a
> draft CEP for it:
>
> http://wiki.cython.org/enhancements/ctypemethods
>
> Basically, it would allow writing this in .pxd files:
>
> """
> cdef extern from "...":
>     cdef cppclass MyClass:
>         cdef int cython_method_here(self):
>             return 1
>
> ctypedef double mydouble:
>     cdef double cython_method_here(self):
>         return self ** 2
>
> cdef struct mystruct:
>     int x, y
>
>     cdef int cython_method_here(self):
>         return self.x + self.y
>
> cdef union myunion:
>     int a
>     double b
>
>     cdef int cython_method_here(self):
>         return self.a if ImSureImAnInt else <int>self.b
> """
>
> The C code for these methods would then be generated into the modules that
> use these types, similar to what we allow with "__getbuffer__()". Calls
> would be direct function calls as with "final" methods.
>
> I think it fits into what's there in a very natural way.
>
> Stefan
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> http://mail.python.org/mailman/listinfo/cython-devel

Looks a bit like C++. I have no strong feelings either way, it would
simplify the memoryview implementation slightly, which special-cases
the memoryview struct with method attributes. I would prefer to keep
the language simple however, and have people write functions operating
on structs. It's really the same thing written differently, and
methods don't really bring an advantage since the data in the struct
cannot be hidden or readonly.


More information about the cython-devel mailing list