List Comprehensions Enhancement

François Pinard pinard at iro.umontreal.ca
Tue Sep 21 17:48:00 EDT 1999


Jeremy Hylton <jeremy at cnri.reston.va.us> writes:

>   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.

Hello, Jeremy.  It surely yields neat code.  Yesterday, I wrote:

    for team in registry.team_list():
        os.path.walk('../trans/%s' % team, walker, (team, domain))

with the corresponding:

def walker((team, domain), dirname, bases):

That is, one can use the "universal" third argument of `os.path.walk'
to make many references available all at once to the walker function,
which theoretically accepts only one.  This is an old trick, of course,
but I found the above writing more clear than for other languages I happen
to remember today :-).

> However, it can lead to the occasional odd error message:
> ValueError: unpack sequence of wrong size

You find it odd?  Only slightly, then.  The message looks rather appropriate
to me.  There are a few situations in Python where there is unpacking magic,
but my impression so far is that they are easily remembered and recognised.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list