operator overloading

Run Settings
LanguagePython
Language Version
Run Command
# -*- coding: utf-8 -*- # the files show how to override the operators in python # example taken from http://blog.teamtreehouse.com/operator-overloading-python # but there are/were some mistakes in the original # the smartest/most complete implementation can be found in adding.py for __add__ # for more see: https://docs.python.org/2/reference/datamodel.html#special-method-names from reverse_adding import BookRAdd as Book_r from adding import BookAdd as Book_a from comparison import BookCompare as Book_comp print "reverse adding" book1 = Book_r('a', 300) book2 = Book_r('b', 400) book3 = Book_r('c', 500) summe = sum([book1, book2, book3]) print summe print "adding" book4 = Book_a('a', 200) book5 = Book_a('b', 300) print book4 + book5 # addings books print book4 + 4 # addings books to int print "comparisons" book6 = Book_comp('a', 30) book7 = Book_comp('b', 31) print "lt: ", book6 < book7 print "le: ", book6 <= book7 print "eq: ", book6 == book7 print "ne: ", book6 != book7 print "ge: ", book6 >= book7 print "gt: ", book6 > book7
# -*- coding: utf-8 -*- # python reverse adding # Basically, remember how 2 + 5 and 5 + 2 are the same thing due to the commutative # property of addition? Python takes advantage of that and swaps the operators. # So instead of 0 + Book, it tries Book + 0. 0 + Book won’t work because the int # class has no idea how to add itself to books. Our Book class can’t do the # reverse add yet but we can give it the ability to. # e.g. sum() uses reverse adding from book import Book class BookRAdd(Book): def __radd__(self, other): return self.pages + other
# -*- coding: utf-8 -*- # adding objects in python # overload the plus operator for a class from book import Book class BookAdd(Book): def __add__(self, other): # you can implement different behavior for different types and objects if isinstance(other, Book): return self.pages + other.pages elif isinstance(other, (int, float)): return self.pages + other else: return NotImplemented # if you only want to add objects of the same type # return self.pages + other.pages
# -*- coding: utf-8 -*- # to make use of the comparison operators for classes # you have to override certain methods # __lt__() -> less than # __le__() -> less than or equal # __eq__() -> equals # __ne__() -> not equals # __ge__() -> greater than or equals # __gt__() -> greater than # you can implement certain comparisons for different types and classes # but you have to know with what you compare your object value # example implementation in adding.py for __add__() from book import Book class BookCompare(Book): def __lt__(self, other): return self.pages < other.pages def __le__(self, other): return self.pages <= other.pages def __eq__(self, other): return self.pages == other.pages def __ne__(self, other): return self.pages != other.pages def __ge__(self, other): return self.pages >= other.pages def __gt__(self, other): return self.pages > other.pages
# -*- coding: utf-8 -*- # the base-class for the examples class Book: title = '' pages = 0 def __init__(self, title='', pages=0): self.title = title self.pages = pages def __str__(self): return self.title
Editor Settings
Theme
Key bindings
Full width
Lines