Tuples in function argument lists
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Sun Jul 17 09:01:38 EDT 2005
I'm trying to understand the use of tuples in function argument lists.
I did this:
>>> def tester(a, (b,c)):
... print a, b, c
...
>>> tester(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in tester
TypeError: unpack non-sequence
That was obvious result.
>>> tester(1, (2, 3))
1 2 3
>>> tester('ab', 'ab')
ab a b
And so were those.
Then I tried this:
>>> def tester(a, (b,c)=None):
... if (b,c) is None:
... print a, None
... else:
... print a, b, c
Needless to say, it did not do what I expected it to do. I didn't expect
it to either :-)
I tried looking at the language reference here:
http://docs.python.org/ref/function.html
but I can't seem to find anything in their that says that tuples-as-args
are legal. Am I misreading the docs, or is this accidental behaviour that
shouldn't be relied on?
Does anyone use this behaviour, and if so, under what circumstances is it
useful?
--
Steven
More information about the Python-list
mailing list