[Python-3000] Changing behavior of sequence multiplication by negative integer (original) (raw)

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:

Agreed.

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 * (minlength- 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 version.

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((minlength- len(seq)), 0)

I would write it as

Create a record-size pad outside the loop

pad = " "*length ... seq = (seq+pad)[:length]

-jJ



More information about the Python-3000 mailing list