cpy
cpy copied to clipboard
Assigning from tuples
The following expression does not work:
function f(a, b)
{
return (a + b, a * b);
}
print f(1, 2);
It gives the error:
line 3:17 mismatched input u',' expecting ')'
line 3:21 no viable alternative at input u'*'
line 3:24 extraneous input u')' expecting ';'
Similarly, this:
x = (1, 2);
gives the error:
line 1:6 missing ')' at u','
line 1:8 no viable alternative at input u'2'
What's the proper way of dealing with tuples in cpy?
Hi, cpy does not support tuples grammar, you can use the tuple function to convert list to tuple:
c = 2;
d = 3;
a = tuple([1+c, 2+d]);
print a;
That's too bad, because it prevents cpy from being able to interface with libraries that return multiple values. Consider a library that returns a tuple. In python, it would be as easy as:
(return_1, return_2) = library.function()
In cpy, would we have to do this?
return_tuple = library.function();
return_1 = return_tuple[0];
return_2 = return_tuple[1];
If so, that's fine, but maybe it should be in the documentation.