unpack sequence to tuple?

John Nielsen nielsenjf at my-deja.com
Wed Jan 24 08:08:39 EST 2001


> Clarity, I guess, is in the eye of the beholder; even if I
> did decide to split this simple function into three statements,
> they'd likely be:
>
> def padSequence(seq, length):
>     temp = list(seq)
>     temp += [None]*length
>     return temp[:length]
>
> at least the [None]*length is clearly simply than starting
> from the (None,)*length tuple then casting it to a list (not
> that you NEED the cast, if .extend is what you want to
> use -- .extend can be called with any sequence).

In python 2.0 yes, in 1.5.2 no :-(

Is there any reason for extend versus +=?

It's odd python 2.0 has what seems like really subtle changes at first.
But, when you start using those changes, you begin to see how they
really help. It's kinda like dictionaries. It's easy to get people to
understand what they mean, but they don't see the utility of it until
they actually use it.


> def padSequence(seq, length):
>     if len(seq)>=length: return seq[:length]
>     return list(seq)+[None]*(length-len(seq))
>
> This is probably the first thing that will come to mind
> to a newbie, after all, and thus might be simplest for
> said newbie to understand.


As usual, you have great taste. That's even clearer.

The first example, though perfectly fine, reminded me of perl. (i.e. I
thought hmm if I showed this to one of my colleagues just picking up
python, would they be able to make sense of it -- tend to have to think
that a lot w/perl).


john


--
nielsenjf at my-Deja.com


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list