[Python-Dev] PEP 343 - next steps
Nick Coghlan
ncoghlan at gmail.com
Sat Jun 11 16:24:18 CEST 2005
Andrew Koenig wrote:
> Of course, this usage shows that the syntax is unnecessary in this context,
> but what I care about is
>
> def f(x as (a, b)):
> # ...
>
> which has the advantage over the alternative
>
> def f(x):
> (a, b) = x
> # ...
>
> that if you call f with the wrong arguments, you get a clearer diagnostic
> message.
Doesn't tuple unpacking give you what you want here already?
Py> def f((a, b)):
... print a, b
...
Py> f(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: f() takes exactly 1 argument (2 given)
Py> f((1, 2))
1 2
A couple of more complex examples:
Py> def f((a, b), (c, d)):
... print a, b, c, d
...
Py> f((1, 2), (3, 4))
1 2 3 4
Py> def f((a, b, (c, d))):
... print a, b, c, d
...
Py> f((1, 2, (3, 4)))
1 2 3 4
About the only downside is the need to rebuild the tuple if you
actually need it.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.blogspot.com
More information about the Python-Dev
mailing list