unpack sequence to tuple?

Alex Martelli aleaxit at yahoo.com
Wed Jan 24 09:45:04 EST 2001


"John Nielsen" <nielsenjf at my-deja.com> wrote in message
news:94mk4k$vhh$1 at nnrp1.deja.com...
>
> > Clarity, I guess, is in the eye of the beholder; even if I
    [snip]
> Is there any reason for extend versus +=?

+= is polymorphic -- it does (roughly) "the right thing" whatever
the exact type of its left-hand operand.  I also find it turns
out to be sometimes clearer/more readable, but that's just me.


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

Actually, I still prefer my first version.  "Eye of the
beholder" thing...


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

I worry less about that in Python -- yes, a big expression
IS more complicated than a series of small statements, e.g.:

def compute(x,y):
    sum = x+y
    product = x*y
    result = (product/sum) + x + y
    return result

vs

def compute(x,y):
    return (x*y)/(x+y) + x + y

but that's a transient effect -- one gets used rather soon
to writing expressions as needed, and only breaking them
down for clarity 'with moderation', and only in somewhat
extreme cases.  Getting Python beginners used to the first
kind of approach (let's only take little baby step at all
times so they can never get confused) is like speaking
'baby talk' to young children -- it may seem natural but
it's not really doing them a favor (IMHO).  That's not to
say one should go out of one's way to produce polysyllabic
obfuscation -- just talk with precision and correct grammar
at all times; the kind of simplicity that IS truly good
for a beginner to get exposed to, is the SAME kind of
simplicity that it's truly good to strive for at all times
(just as the kind of language that I think one should try
to use with one's kids is the same precise and correct one
that one would ideally use at all times).


Alex






More information about the Python-list mailing list