converting from perl: variable sized unpack

Thomas Wouters thomas at xs4all.net
Sun Jul 15 06:54:01 EDT 2001


On Sat, Jul 14, 2001 at 05:12:25PM -0500, Skip Montanaro wrote:

> >>>>> "RS" == Roy Smith <roy at panix.com> writes:

>     RS> What's the best way to translate this perl snippet into python:

>     RS> ($f1, $f2, $f3, $f4, $f5, $f6) = split ('\s+', $line);

>     BAW> Python doesn't work this way, but I've been thinking about writing
>     BAW> a PEP to make sequence unpacking less strict.

> What about writing a simple class to do it?  Instead of raising an exception
> in __getitem__ it would just return "" or None.

Much though I would like to see 'loose' tuple unpacking, I have to admit
Skip is right: it's easy to do with a wrapper class, or even better, a
generator:

def loose(l, maxlen, pad=None):
    for item in (l + maxlen*[pad])[:maxlen]:
        yield item

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list