[Python-Dev] Re: Inplace multiply

Terry Reedy tjreedy@udel.edu
Tue, 13 May 2003 15:12:50 -0400


"Jeff Epler" <jepler@unpythonic.net> wrote in message
news:20030513183144.GH11289@unpythonic.net...
> There must be something more to your problem than what you
described.
>
> The following executes just fine for me (ditto if NewKoke is a
subclass
> of object instead of list, and no matter whether I define
__getitem__ or
> not [a guess based on your remark about 'multiply a sequence']):
>
> $ python dubois.py
> sausages vegetable-style breakfast patty
> sausages vegetable-style breakfast patty

On Win98 2.2.1, cut and paste into interactive window outputs
sausages vegetable-style breakfast patty
sausages []

> class Klassic:
>     def __imul__(self, other):
>         return "sausages"
>     def __getitem__(self, i): return None
>
> class NewKoke(list):
>     def __imul__(self, other):
>         return "vegetable-style breakfast patty"
>     def __getitem__(self, i): return None
>
> k = Klassic()
> o = NewKoke()
>
> k *= 1
> o *= 1
>
> print k, o
>
> k = Klassic()
> o = NewKoke()
>
> k *= "spam"
> o *= "spam"

Because line above gives
TypeError: can't multiply sequence to non-int

> print k, o

Maybe something has been 'fixed' since then.

Terry J. Reedy