pattern matching in python

Fredrik Lundh fredrik at effbot.org
Fri Jan 12 03:00:38 EST 2001


Quinn Dunkan wrote:
> I recently noticed, that, of course,
>
> def f(a, (x, y), b): ...
>
> works too.  Is this standard, documented behaviour that
> can be relied on?

It's part of the def statement's grammar:

sublist:        parameter ("," parameter)* [","]
parameter:      identifier | "(" sublist ")"

(see http://www.python.org/doc/current/ref/function.html
for the full grammar)

But as far as I can tell, it's not discussed anywhere else in
the language reference.  The "calls" chapter talks alot about
how an argument tuple is formed, but doesn't say anything
about how that tuple is mapped to the function's formal
parameters.

(but it definitely qualifies as "standard behaviour", even
if it's only partially documented.  but iirc, it has been on
the list of things that may change in "Python 3000"...)

Cheers /F





More information about the Python-list mailing list