Question about overloading of binary operators

R

Raj Bandyopadhyay

Hi

Here's a simple class example I've defined

#############################
class myInt(int):
def __add__(self,other):
return 0

print 5 + myInt(4) #prints 9
print myInt(4) + 5 #prints 0
#############################

The Python binary operation function (binary_op1() in
Objects/abstract.c) states
the rules for binary operations as follows:

v w Action
-------------------------------------------------------------------
new new w.op(v,w)[*], v.op(v,w), w.op(v,w)
new old v.op(v,w), coerce(v,w), v.op(v,w)
old new w.op(v,w), coerce(v,w), v.op(v,w)
old old coerce(v,w), v.op(v,w)

[*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
v->ob_type


It seems that my example should fall in case 1, and in both cases, the
__add__ function of the subclass should be used, returning 0, regardless
of operand order. However, in one case the subclass's function is used
and not in the other case. What am I missing here?

Thanks
Raj
 
G

gigs

Raj said:
Hi

Here's a simple class example I've defined

#############################
class myInt(int):
def __add__(self,other):
return 0

print 5 + myInt(4) #prints 9
print myInt(4) + 5 #prints 0
#############################

The Python binary operation function (binary_op1() in
Objects/abstract.c) states
the rules for binary operations as follows:

v w Action
-------------------------------------------------------------------
new new w.op(v,w)[*], v.op(v,w), w.op(v,w)
new old v.op(v,w), coerce(v,w), v.op(v,w)
old new w.op(v,w), coerce(v,w), v.op(v,w)
old old coerce(v,w), v.op(v,w)

[*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
v->ob_type


It seems that my example should fall in case 1, and in both cases, the
__add__ function of the subclass should be used, returning 0, regardless
of operand order. However, in one case the subclass's function is used
and not in the other case. What am I missing here?

Thanks
Raj
i think that you need to use __radd__ for addition with custom object on right
 
D

Dan Bishop

Raj said:
Here's a simple class example I've defined
#############################
class myInt(int):
def __add__(self,other):
return 0
print 5 + myInt(4) #prints 9
print myInt(4) + 5 #prints 0
#############################
The Python binary operation function (binary_op1() in
Objects/abstract.c) states
the rules for binary operations as follows:
v w Action
-------------------------------------------------------------------
new new w.op(v,w)[*], v.op(v,w), w.op(v,w)
new old v.op(v,w), coerce(v,w), v.op(v,w)
old new w.op(v,w), coerce(v,w), v.op(v,w)
old old coerce(v,w), v.op(v,w)
[*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
v->ob_type
It seems that my example should fall in case 1, and in both cases, the
__add__ function of the subclass should be used, returning 0, regardless
of operand order. However, in one case the subclass's function is used
and not in the other case. What am I missing here?
Thanks
Raj

i think that you need to use __radd__ for addition with custom object on right

Right. Python doesn't make any assumptions that addition is
commutative.

But if it is for your class, you can just write

__radd__ = __add__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top