tags:

views:

251

answers:

3

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?

+5  A: 

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.

FogleBird
I'm pretty sure that's the exact example that Hank Gay saw "exotic signatures" used. A single example is not a very good demonstration of what a term encompasses.
Daniel Lew
Yes, I did Google. That was the reference. It's not clear to me what makes that signature "exotic".
Hank Gay
I thought the example made it straight-forward. (I had never heard the term myself) I edited my answer with more detail.
FogleBird
Thanks. Swapped my vote.
Hank Gay
That's interesting^^ What Python calls exotic is done in functional programming languages all day long (passing + decomposing patterns and tupels in arguments). These do even use currying to make their function signatures more confusing ;-)
Dario
@Dario: currying is available in Python too, see functools.partial at http://docs.python.org/library/functools.html#functools.partial . And btw, a person calling them "exotic" does not mean that Python documentation calls them "exotic".
ΤΖΩΤΖΙΟΥ
+5  A: 

More information about tuple parameter unpacking (and why it is removed) here: http://www.python.org/dev/peps/pep-3113/

truppo
+1  A: 

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 :-)

John Fouhy