Haskell like (c:cs) syntax
stefan at 3niemann.de
Wed Aug 29 22:53:16 CEST 2007
Thanks for all the good answers.
In fact the `Extended Iterable Unpacking' is exactly what I was looking for.
Ok, my main aspect of writing
head, *tail = seq
head, tail = seq, seq[1:]
is the syntactic sugar. As mentioned in the PEP this may also be faster when
iterables are involved.
"Matimus" <mccredie at gmail.com> schrieb im Newsbeitrag
news:1188349012.502720.296440 at x40g2000prg.googlegroups.com...
>> Is there a pattern matching construct in Python like (head : tail),
>> 'head' matches the first element of a list and 'tail' matches the rest? I
>> could not find this in the Python documentation.
> Not really, but you could do something like this:
> def foo(head, *tail):
> #do stuff with head and tail
> Also, Python 3.0 will have `Extended Iterable Unpacking'
> This isn't quite the same as Haskell's type matching, but would enable
> similar behavior in some cases.
> head, *tail = seq
> Which would assign the first element of seq to head, and the remainder
> to tail.
More information about the Python-list