I would like the operator of my class to interact with regular types in the way that I define. Say, for example, I have:
class Mynum(object):
def __init__(self, x):
self.x = x
def __add__(self, other):
return self.x + other.x
a = Mynum(1)
b = Mynum(2)
print a+b
This works fine, but now if I try to do:
print a+2
I am getting an error because it intdoes not have a member with a name x. How to define Mynum+ intin a class? It sounds like work for decorators or metaclasses, but I'm terribly unfamiliar with using them. This question seems similar, but not exactly identical.
source
share