[Python-Dev] Inplace multiply

Jeff Epler jepler@unpythonic.net
Tue, 13 May 2003 13:31:45 -0500


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

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"

print k, o