List Comprehensions Enhancement
Jeremy Hylton
jeremy at cnri.reston.va.us
Tue Sep 21 16:07:31 EDT 1999
>>>>> "ms" == skaller <skaller at maxtal.com.au> writes:
ms> In case you didn't know -- and hey, I didn't either until
ms> recently -- Python ALREADY has pattern matching, its just that
ms> the patterns are simple: the only patterns recognized are
ms> variable-name tuple of pattern
ms> For example:
ms> def f((x,y,z)): print x,y,z f((1,2,3))
I've used this feature very occasionally, and it seemed marginally
helpful. However, it can lead to the occasional odd error message:
>>> def times((a,b)):
... return 42
...
>>> times((6,9))
42
>>> times("huh?")
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in times
ValueError: unpack sequence of wrong size
Strings are sequences, and sequences are unpacked for the function. I
believe the error message is more obscure when it's created by an
extension module using PyArg_ParseTuple.
Jeremy
More information about the Python-list
mailing list