[Python-3000] Changing behavior of sequence multiplication by negative integer

Jim Jewett jimjjewett at gmail.com
Thu Aug 10 16:13:14 CEST 2006

Lawrence Oluyede wrote:

> seq * -5
> and to be honest I've never seen code like that because the semantics
> is somewhat senseless to me

To be honest, I would almost expect the negative to mean "count from
the end", so that it also reversed the sequence.  It doesn't, but ...
it does make for a hard-to-explain case.

> ... evaluation of "Sequence * negative integer" should be changed from:

> >>> "foobar" * -1
>  ''

> > ... to something throwing an exception like when you try to multiplicate
> > the sequence by a floating point number:


On 8/10/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
> The "negative coerced to 0" behaviour is to make it easy to do things like
> padding a sequence to a minimum length:

>    seq = seq + pad * (min_length- len(seq))

Typically, if I need to pad a sequence to a minimum length, I really
need it to be a specific length.  Having it already be too long is
likely to cause problems later.  So I really do prefer the explicit

Also compare this to the recent decision that __index__ should *not*
silently clip to a C long

> Without the current behaviour, all such operations would need to be rewritten as:

>    seq = seq + pad * max((min_length- len(seq)), 0)

I would write it as

# Create a record-size pad outside the loop
pad = " "*length
    seq = (seq+pad)[:length]


More information about the Python-3000 mailing list