Return type of multiple values

1,477 views
Skip to first unread message

Gökhan Sever

unread,
May 28, 2011, 7:16:23 PM5/28/11
to cython-users
Hello,

Considering the following simple example:

%cython

cdef int a,b,c,d

a, b, c, d = myfunc(1,2,3,4)
a = a + b +c +d

cpdef myfunc(int x1, int x2, int x3, int x4):
return x1*x1, x2*x2, x3*x3, x4*x4

Is there a way of declaring types for multiple return values? Say
that
I want to assert all return types as integers. Is this possible?

Thanks.

Stefan Behnel

unread,
May 28, 2011, 11:44:36 PM5/28/11
to cython...@googlegroups.com
Gökhan Sever, 29.05.2011 01:16:

There's not much use in doing that because they are passed back as tuple
anyway, so they are always converted to Python objects.

It's worth declaring the return type as tuple, though.

Stefan

Robert Bradshaw

unread,
May 29, 2011, 12:39:24 AM5/29/11
to cython...@googlegroups.com

We have talked about allowing a return of, e.g, (int, int, int, int)
which woud get packed as a struct in C (and as a tuple in Python for
cpdef functions).

- Robert

Stefan Behnel

unread,
May 29, 2011, 2:53:26 AM5/29/11
to cython...@googlegroups.com
Robert Bradshaw, 29.05.2011 06:39:

That would be nice, yes. It would easily work for C types (as above), but
we don't currently support Python objects in structs (IIRC) and that's
semantically tricky, so there may be limits to what we can do with this
approach.

Stefan

Gökhan Sever

unread,
May 29, 2011, 5:56:08 PM5/29/11
to cython-users


On May 28, 10:39 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:
This will be useful for my case. See the actual code at ->
http://gandalf.aero.und.edu/~gsever/modellib.html

The function "growth" (within parcel_model_12 function) returns 4
values each time executed. I see a lot of PyFloat type of conversions
both from the growth function return and assigning these values into
previously cdef typed arrays and scalar. Knowing the exact type of
return values and eliminating these PyFloat type conversions will most
likely give me a bit more speed-up.

@Stefan, Yes I use tuple as a return value "cpdef tuple growth(..."
This makes a slight change in the code that Cython produces and a very
slight speed-up in the code execution.

Robert Bradshaw

unread,
Jul 3, 2011, 1:09:28 AM7/3/11
to cython...@googlegroups.com

In the meantime, you can return a struct and manually unpack it to
achieve the same effect.

Any objections to the

cdef (int, double, object) foo():
...

a, b, c = foo()

syntax? The tricky part is if/how to do recounting on

a = b = foo()

I suppose, given that this is a special, implicit type that all
assignments would be handled by Cython.

- Robert

Stefan Behnel

unread,
Jul 3, 2011, 2:54:54 AM7/3/11
to cython...@googlegroups.com
Robert Bradshaw, 03.07.2011 07:09:

> Any objections to the
>
> cdef (int, double, object) foo():
> ...
>
> a, b, c = foo()
>
> syntax?

I think the same as for

def func((a,b)): ...

applies here (bug 692).

Stefan

Reply all
Reply to author
Forward
0 new messages