45

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 <<?

Community
  • 1
  • 1
Ahmad Dwaik
  • 963
  • 1
  • 9
  • 13
  • 2
    Duplicate question: http://stackoverflow.com/questions/932328. See also http://stackoverflow.com/questions/1552260 . – Peter Hansen Dec 20 '09 at 15:37
  • 2
    It may be a duplicate question, but I found the accepted answer more useful than in other questions, because it links to the description of all the __ xxx __ methods. – ToolmakerSteve Nov 23 '13 at 22:58
  • @ToolmakerSteve - Sorry, I flagged and removed that as not an answer. It was nothing more than a link, which means it was subject to linkrot. – ArtOfWarfare Aug 28 '14 at 21:55
  • 1
    Since this is the first hit on google, I think it should be unlocked. Locking it forces people to have an extra click to get information and the answers below don't even give examples. – Chris Redford Nov 11 '14 at 18:13

3 Answers3

72

As other answers have mentioned, you can indeed overload operators (by definining special methods in the class you're writing, i.e., methods whose names start and end with two underscores). All the details are here.

To complete the answers to you questions: you cannot define new operators; but << is not a new operator, it's an existing one, and it's overloaded by defining in the class the method __lshift__.

As a historical note, this is also pretty much the situation in C++ -- but the exact set of operators you can overload differs between the two languages. For example, in C++, you cannot overload attribute access, .; in Python, you can, with __getattr__ (or __getattribute__, with different semantics) and __setattr__. Vice versa, in Python = (plain assignment) is not an operator, so you cannot overload that, while in C++ it is an operator and you can overload it.

<< is an operator, and can be overloaded, in both languages -- that's how << and >>, while not losing their initial connotation of left and right shifts, also became I/O formatting operators in C++ (not in Python!-).

taper
  • 528
  • 5
  • 22
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • +1 for the link to the list of __ xxx __ methods. I couldn't find that link in answers to other questions that this question is considered a duplicate of. – ToolmakerSteve Nov 23 '13 at 22:56
6

See: http://docs.python.org/reference/datamodel.html#special-method-names.

A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators.

miku
  • 181,842
  • 47
  • 306
  • 310
4

Yes, and no. I don't think you can define your own operators, but you can overload the existing ones - you can do that by overriding the operator's special method. For example, to override >, you can override __gt__(), for != override __ne__() and so on.

Sudhir Jonathan
  • 16,998
  • 13
  • 66
  • 90