Monday, November 18, 2013

Tuple parameter unpacking in Python functions


Until yesterday I didn't know that I could use tuple parameter unpacking in function definitons:
2.7.5+ (default, Sep 19 2013, 13:48:49) 
>>> def func((a, (b, c), d), e):
...     print a, b, c, d, e
... 
>>> args = (('a', ('b', 'c'), 'd'), 'e')
>>> func(*args)
a b c d e
>>> 
I have never needed this.

It turns out this feature was removed in Python3:
Python 3.3.2+ (default, Oct  9 2013, 14:50:09) 
>>> def func((a, (b, c), d), e):
  File "<stdin>", line 1
    def func((a, (b, c), d), e):
             ^
SyntaxError: invalid syntax
>>> 
And I agree with the removal.

1 comment:

  1. I like your blog, I read this blog please update more content on python, further check it once at python online course

    ReplyDelete