Overloaded function

121 views
Skip to first unread message

Ian Bell

unread,
Aug 6, 2012, 11:55:36 PM8/6/12
to cython...@googlegroups.com
Is there any way to do overloaded functions in cython?  I have a PXD file that demonstrates what I am trying to do:

cdef extern from "CoolProp.h":

    double _Props "Props"(char *Output,char Name1, double Prop1, char Name2, double Prop2, char * Ref)
    double _Props1 "Props"(char * Ref, char *Output)
   
#Function prototypes
cpdef double Props(bytes Parameter, bytes param1, float value1, bytes param2, float value2, bytes Fluid)
cpdef double Props(bytes Fluid, bytes Parameter)

I would then write my implementation in a PYX file and all should be well.

I can get this to work with SWIG without problems, but I am getting progressively more annoyed with SWIG in general and am hoping to move all my SWIG code to cython and get rid of SWIG entirely throughout all my projects.

Thanks,
Ian

Stefan Behnel

unread,
Aug 7, 2012, 3:45:57 AM8/7/12
to cython...@googlegroups.com
Ian Bell, 07.08.2012 05:55:
> Is there any way to do overloaded functions in cython? I have a PXD file
> that demonstrates what I am trying to do:
>
> cdef extern from "CoolProp.h":
>
> double _Props "Props"(char *Output,char Name1, double Prop1, char
> Name2, double Prop2, char * Ref)
> double _Props1 "Props"(char * Ref, char *Output)
>
> #Function prototypes
> cpdef double Props(bytes Parameter, bytes param1, float value1, bytes
> param2, float value2, bytes Fluid)
> cpdef double Props(bytes Fluid, bytes Parameter)
>
> I would then write my implementation in a PYX file and all should be well.

Hmm, you mean that you want to implement these in Cython and export them to
other modules? This is not about wrapping C++ implemented code, right?

Cython doesn't currently support this. You can use optional arguments in
cdef functions, though.

Such a feature can't be hard to add, given that we have already support for
C++ types anyway. You wouldn't declare a cname though (your "Props" string
above), Cython would mangle the internal names for you. However, exporting
them so that other modules can use them might turn out to be tricky because
all Cython versions you use would have to come up with the same mangled
public name.

Stefan

mark florisson

unread,
Aug 7, 2012, 5:27:23 AM8/7/12
to cython...@googlegroups.com
Maybe you just want to use default values?

Ian Bell

unread,
Aug 7, 2012, 12:39:45 PM8/7/12
to cython...@googlegroups.com
On Tue, Aug 7, 2012 at 3:45 AM, Stefan Behnel <stef...@behnel.de> wrote:
Ian Bell, 07.08.2012 05:55:
> Is there any way to do overloaded functions in cython?  I have a PXD file
> that demonstrates what I am trying to do:
>
> cdef extern from "CoolProp.h":
>
>     double _Props "Props"(char *Output,char Name1, double Prop1, char
> Name2, double Prop2, char * Ref)
>     double _Props1 "Props"(char * Ref, char *Output)
>
> #Function prototypes
> cpdef double Props(bytes Parameter, bytes param1, float value1, bytes
> param2, float value2, bytes Fluid)
> cpdef double Props(bytes Fluid, bytes Parameter)
>
> I would then write my implementation in a PYX file and all should be well.

Hmm, you mean that you want to implement these in Cython and export them to
other modules? This is not about wrapping C++ implemented code, right?

Yeah, that is exactly what I want to do.  I could probably live without exporting them, but if I compile a file as a PYX file, I lose the ability to do something like::

    Props(*args)

and then select the right function signature based on the form of args.  Also this will take a significant speed penalty since I need to be doing type comparison, and this is a function that gets called quite a few million times, so performance is of the utmost importance.
 
Cython doesn't currently support this. You can use optional arguments in
cdef functions, though.

Optional, yes, but so far as I am aware, no way to do something like

    cpdef double Props(*args):

Ian Bell

unread,
Aug 8, 2012, 12:42:49 PM8/8/12
to cython...@googlegroups.com
For others that follow, what I ended up doing was to just create a shim function with the signature::
   
    Props(bytes in1, bytes in2, in3 = None, in4 = None, in5 = None, in6 = None)

and then called the correct function from the CoolProp header as needed.  Thanks for the idea about default parameters.

Ian
Reply all
Reply to author
Forward
0 new messages