pythonpython-2.7pattern-matchingtuplesfunction-signature

placing python tuples in function signature


In python there is this interesting, and very useful tool by which you can pattern match values from tuples on function signature.

def first((a, b)):
    return a

x = (4, 9)
first(x)
li = [(5, 4), (8, 9)]
map(first, li)

def second(a, b):
    # does not work the same way
    return b

I don't see any literature on use of this. What is the vocabulary the python community uses for this? Is there a compelling reason to not use this?


Solution

  • It's called tuple parameter unpacking and was removed in Python 3.0.

    Like @zondo said, you might not want to use it for compatibility reasons. I myself still use it occasionally in Python 2. You'll find reasons against it in the PEP of my first link, though keep in mind that those are the reasons it got removed from the language, and I think it was at least partially because it made things easier for the Python makers, which is not necessarily a reason for you or me to avoid it.