I recently saw a reference to "exotic signatures" and the fact they had been deprecated in 2.6 (and removed in 3.0). The example given is
def exotic_signature((x, y)=(1,2)): return x+y
What makes this an "exotic" signature?
I recently saw a reference to "exotic signatures" and the fact they had been deprecated in 2.6 (and removed in 3.0). The example given is
def exotic_signature((x, y)=(1,2)): return x+y
What makes this an "exotic" signature?
What's exotic is that x and y represent a single function argument that is unpacked into two values... x and y. It's equivalent to:
def func(n):
x, y = n
...
Both functions require a single argument (list or tuple) that contains two elements.
More information about tuple parameter unpacking (and why it is removed) here: http://www.python.org/dev/peps/pep-3113/
Here's a slightly more complex example. Let's say you're doing some kind of graphics programming and you've got a list of points.
points = [(1,2), (-3,1), (4,-2), (-1,5), (3,3)]
and you want to know how far away they are from the origin. You might define a function like this:
def magnitude((x,y)):
return (x**2 + y**2)**0.5
and then you can find the distances of your points from (0,0) as:
map(magnitude, points)
...well, at least, you could in python 2.x :-)