
My "masked array" class MA has a problem that I don't know how to solve. The inplace multiply function def __imul__ (self, other) is not getting called while my other input operations do work. The scenario is x = MA.array(...) x *= c If c is an int, this works correctly, calling MA.__imul__. Otherwise, I get a message from the Python runtime saying it can't multiply a sequence by a non-int. But change MA to Numeric, it works. Numeric is an extension type and MA is a (new style) class. MA defines __len__ as well as all the math operators.

My "masked array" class MA has a problem that I don't know how to solve. The inplace multiply function
def __imul__ (self, other)
is not getting called while my other input operations do work. The scenario is
x = MA.array(...)
x *= c
If c is an int, this works correctly, calling MA.__imul__. Otherwise, I get a message from the Python runtime saying it can't multiply a sequence by a non-int. But change MA to Numeric, it works.
Numeric is an extension type and MA is a (new style) class. MA defines __len__ as well as all the math operators.
We won't be able to help without seeing your code. --Guido van Rossum (home page: http://www.python.org/~guido/)

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

"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

"Paul Dubois" <paul@pfdubois.com> writes:
My "masked array" class MA has a problem that I don't know how to solve. The inplace multiply function
def __imul__ (self, other)
is not getting called while my other input operations do work. The scenario is
x = MA.array(...)
x *= c
If c is an int, this works correctly, calling MA.__imul__. Otherwise, I get a message from the Python runtime saying it can't multiply a sequence by a non-int. But change MA to Numeric, it works.
Numeric is an extension type and MA is a (new style) class. MA defines __len__ as well as all the math operators.
What version of Python? This smells like a bug that has been (thought) fixed. Cheers, M. -- The ability to quote is a serviceable substitute for wit. -- W. Somerset Maugham
participants (5)
-
Guido van Rossum
-
Jeff Epler
-
Michael Hudson
-
Paul Dubois
-
Terry Reedy