Problem with pure Python mode class constructors type definitions

78 views
Skip to first unread message

Ian Bell

unread,
May 1, 2012, 10:31:24 PM5/1/12
to cython...@googlegroups.com
Unless I am missing something, it does not seem to be possible to apply type definitions for a class constructor in a .pxd file.  In my pure Python file I can do something like

import cython

class A:

    @cython.locals(x=cython.double)
    def __init__(self,x):
        pass

but if I try to make a type definition in the PXD file like

cdef class A:

    def __init__(self, double x)

and compile I get an error:

Error compiling Cython file:
------------------------------------------------------------
...

cdef class A:

    def __init__(self, double x)                               ^
------------------------------------------------------------

A.pxd:4:32: Expected ':', found 'NEWLINE'

Is there some special syntax that I am missing?  I can get this to work for the "normal" functions without a problem.

Regards,
Ian

Robert Bradshaw

unread,
May 2, 2012, 2:15:22 AM5/2/12
to cython...@googlegroups.com
__init__ is a standard python method, and should be simply left out of
the declaration in your .pxd file. Only c(p)def functions belong
there.

- Robert

Ian Bell

unread,
May 2, 2012, 3:24:59 AM5/2/12
to cython...@googlegroups.com
Robert,

So the only way to provide typedefs for parameters in __init__ is in the actual pure Python mode .py file then?  This is not exactly ideal if you are trying to be consistent about splitting implementation (in the .py or .pyx file) and declaration (in the .pxd file).

Regards,
Ian

Robert Bradshaw

unread,
May 2, 2012, 3:54:18 AM5/2/12
to cython...@googlegroups.com
True.

> This is not exactly ideal if you are
> trying to be consistent about splitting implementation (in the .py or .pyx
> file) and declaration (in the .pxd file).

Fair enough, though

def __init__(self, double x):
...

is really just shorthand for

def __init__(self, arg):
double x = arg
...

so it really is an implementation detail (because python functions
don't really have signatures).

- Robert
Reply all
Reply to author
Forward
0 new messages