Python style advice
tjreedy at udel.edu
Tue Feb 10 06:02:26 CET 2004
"Nick Craig-Wood" <ncw at axis.demon.co.uk> wrote in message
news:slrnc2enr8.bmp.ncw at irishsea.home.craig-wood.com...
> Python newbie advice needed!
> I'm tring to write what I would have expressed in Perl as
> my ($a, $b, $c) = @array;
> This is very similar to the python statement
> a, b, c = array
> BUT, the Python will throw an exception if array isn't exactly 3 elements
This is an intentional feature. Mismatches are often bugs.
> wheras the Perl will work for any length of @array,
> either throwing away excess elements or setting the variables to
> undef, ie like this
Guido's philosophy is that the interpreter should resist guessing like this
when code is at least half likely to be buggy.
> And this works however many elements array has
> a, b, c = (array + 3*[None])[:3]
> but it doesn't seem very Pythonic - is there a better way?
Being explicitly generic is Pythonic to me. Or do something like
but I prefer the one liner here. It is easier to extend to more names.
Terry J. Reedy
More information about the Python-list