Is __mul__ sufficient for operator '*'?
malkarouri at gmail.com
Tue Oct 20 02:31:44 CEST 2009
I was having a go at a simple implementation of Maybe in Python when I
stumbled on a case where x.__mul__(y) is defined while x*y is not.
The class defining x is:
def __init__(self, obj):
self.o = obj
return 'Maybe(%s)' % object.__getattribute__(self, "o")
def __getattribute__(self, name):
o = object.__getattribute__(self, "o")
r = getattr(o,name)
f = lambda *x:Maybe(r(*x))
The code exercising this class is:
Traceback (most recent call last):
File "<pyshell#83>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'Maybe' and 'int'
The farthest I can go in this is that I presume that __mul__ (as
called by operator *) is supposed to be a bound method while I am
returning a lambda function. Is this correct? And How can I make the
implementation support such operators?
More information about the Python-list