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

Nick Coghlan ncoghlan at gmail.com
Thu Aug 10 13:40:32 CEST 2006


Lawrence Oluyede wrote:
> I've never seen bugs determined by operations such as:
> 
> "foobar" * -1
> 
> and to be honest I've never seen code like that because the semantics
> is somewhat senseless to me but I think the behavior of the expression
> evaluation of "Sequence * negative integer" should be changed from:
> 
>>>> "foobar" * -1
> ''
>>>> ["foobar"] * -1
> []
>>>> ("foobar") * -1
> ''
> 
> to something throwing an exception like when you try to multiplicate
> the sequence by a floating point number:
> 
>>>> "foobar" * 1.0
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: can't multiply sequence by non-int
> 
> It's not a big deal to me but maybe this can be addressed in the
> python3000 branch
> 

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

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

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

Gratuitous breakage that leads to a more verbose result gets a solid -1 from me :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list