default value in a list

Nick Craig-Wood nick at craig-wood.com
Sat Jan 22 14:30:04 EST 2005


Alex Martelli <aleaxit at yahoo.com> wrote:
>  Nick Craig-Wood <nick at craig-wood.com> wrote:
>     ...
> > Or this version if you want something other than "" as the default
> > 
> >   a, b, b = (line.split(':') + 3*[None])[:3]
> 
>  Either you mean a, b, c -- or you're being subtler than I'm
>  grasping.

Just a typo - I meant c!

> > BTW This is a feature I miss from perl...
> 
>  Hmmm, I understand missing the ``and all the rest goes here'' feature
>  (I'd really love it if the rejected
>      a, b, *c = whatever
>  suggestion had gone through, ah well), but I'm not sure what exactly
>  you'd like to borrow instead -- blissfully by now I've forgotten a lot
>  of the perl I used to know... care to clarify?

I presume your construct above is equivalent to

  my ($a, $b, @c) = split /.../;

which I do indeed miss.

Sometimes I miss the fact that in the below any unused items are set
to undef, rather than an exception being raised

  my ($a, $b, $c) = @array;

However, I do appreciate the fact (for code reliability) that the
python equivalent

  a, b, c = array

will blow up if there aren't exactly 3 elements in array.

So since I obviously can't have my cake an eat it here, I'd leave
python how it is for the second case, and put one of the suggestions
in this thread into my toolbox / the standard library.

BTW I've converted a lot of perl programs to python so I've come
across a lot of little things like this!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list