operator-overloading

method overloading python

method overloading python Question: This is similar to method overloading. When print Hello(1, 2, 3) gets executed it returns “a and b” whereas I want it to return “a and b and c”. I know I could have said if (a and b and c is None) then it would have worked. But if I …

Total answers: 5

Operator overloading in python with the object on the right hand side of the operator

Operator overloading in python with the object on the right hand side of the operator Question: I recently learned about operator overloading in python and I would like to know if the following is possible. Consider the following hypothetical/contrived class. class My_Num(object): def __init__(self, val): self.val = val def __add__(self, other_num): if isinstance(other_num, My_Num): return …

Total answers: 2

Why can't I add a tuple to a list with the '+' operator in Python?

Why can't I add a tuple to a list with the '+' operator in Python? Question: Python not support adding a tuple to a list: >>> [1,2,3] + (4,5,6) Traceback (most recent call last): File “<stdin>”, line 1, in <module> TypeError: can only concatenate list (not “tuple”) to list What are the disadvantages for providing …

Total answers: 3

Why does Python have an __ne__ operator method instead of just __eq__?

Why does Python have an __ne__ operator method instead of just __eq__? Question: The answer here gives a handwaving reference to cases where you’d want __ne__ to return something other than just the logical inverse of __eq__, but I can’t imagine any such case. Any examples? Asked By: Jegschemesch || Source Answers: Some libraries do …

Total answers: 3

Why/When in Python does `x==y` call `y.__eq__(x)`?

Why/When in Python does `x==y` call `y.__eq__(x)`? Question: The Python docs clearly state that x==y calls x.__eq__(y). However it seems that under many circumstances, the opposite is true. Where is it documented when or why this happens, and how can I work out for sure whether my object’s __cmp__ or __eq__ methods are going to …

Total answers: 4

Override Python's 'in' operator?

Override Python's 'in' operator? Question: If I am creating my own class in Python, what function should I define so as to allow the use of the in operator, e.g. class MyClass(object): … m = MyClass() if 54 in m: … Asked By: astrofrog || Source Answers: MyClass.__contains__(self, item) Answered By: Ignacio Vazquez-Abrams A more …

Total answers: 3

How to override the [] operator in Python?

How to override the [] operator in Python? Question: What is the name of the method to override the [] operator (subscript notation) for a class in Python? Asked By: Sahas || Source Answers: You need to use the __getitem__ method. class MyClass: def __getitem__(self, key): return key * 2 myobj = MyClass() myobj[3] #Output: …

Total answers: 3

operator overloading in python

operator overloading in python Question: Possible Duplicates: Python: defining my own operators? Rules of thumb for when to use operator overloading in python Is it possible to overload operators in Python? If so, can one define new operators, such as ++ and <<? Asked By: Ahmad Dwaik || Source Answers: See: http://docs.python.org/reference/datamodel.html#special-method-names. A class can …

Total answers: 3

__lt__ instead of __cmp__

__lt__ instead of __cmp__ Question: Python 2.x has two ways to overload comparison operators, __cmp__ or the “rich comparison operators” such as __lt__. The rich comparison overloads are said to be preferred, but why is this so? Rich comparison operators are simpler to implement each, but you must implement several of them with nearly identical …

Total answers: 5

Is there a way to overload += in python?

Is there a way to overload += in python? Question: I know about the __add__ method to override plus, but when I use that to override +=, I end up with one of two problems: (1) if __add__ mutates self, then z = x + y will mutate x when I don’t really want x …

Total answers: 1