Unpacking extension (Re: A small inconsistency in syntax?)

Greg Ewing greg at cosc.canterbury.ac.nz
Tue Oct 30 22:52:17 EST 2001


Bernhard Herzog wrote:
> 
> p = Pair(17, 42)
> a, b = p
> 
> The only thing it doesn't do is checking whether p is an instance of
> Pair as your proposal would but that's better done with a separate test,
> anyway, IMO.

This is actually part of a larger idea I've been
thinking about for a while, and that's to give
Python some of the flavour of the pattern-matching
case statements found in functional languages,
so you can write things like

   case foo:
     Nothing():
       print "We got nada"
     Pair(Furble(p, q), Furble(r, s)):
       print "We got a pair of furbles"
     Pair(a, b):
       print "We got a pair of something else"
     x:
       print "We got something else"

Of course, you could always write out the tests using
if-elif-else. But note the distinction between the
second and third cases, which would be somewhat tedious
to write out that way.

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list