Re: Is __mul__ sufficient for operator '*'?

2009-10-25 Thread Mick Krippendorf
Muhammad Alkarouri schrieb: 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. class Maybe(object): def __init__(self, obj): self.o = obj def __repr__(self): return 'Maybe(%s)' %

Re: Is __mul__ sufficient for operator '*'?

2009-10-20 Thread Mick Krippendorf
Gabriel Genellina schrieb: http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes Ok. That explains a lot. And your explanation tells the rest. Thank you. In short, you have to define the __mul__ method on the type itself or any of its bases. I found this,

Is __mul__ sufficient for operator '*'?

2009-10-19 Thread Muhammad Alkarouri
Hi everyone, 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: class Maybe(object): def __init__(self, obj): self.o = obj def __repr__(self): return

Re: Is __mul__ sufficient for operator '*'?

2009-10-19 Thread Gary Herron
Muhammad Alkarouri wrote: Hi everyone, 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: class Maybe(object): def __init__(self, obj): self.o = obj def

Re: Is __mul__ sufficient for operator '*'?

2009-10-19 Thread Gabriel Genellina
En Mon, 19 Oct 2009 21:31:44 -0300, Muhammad Alkarouri malkaro...@gmail.com escribió: 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. __special__ methods are searched in the type, not in the instance

Re: Is __mul__ sufficient for operator '*'?

2009-10-19 Thread Mick Krippendorf
Muhammad Alkarouri schrieb: Traceback (most recent call last): File pyshell#83, line 1, in module x*7 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

Re: Is __mul__ sufficient for operator '*'?

2009-10-19 Thread Mick Krippendorf
Gabriel Genellina schrieb: __special__ methods are searched in the type, not in the instance directly. x*y looks for type(x).__mul__ (among other things) So I thought too, but: class meta(type): def __mul__(*args): return 123 class boo(object): __metaclass__ = meta print