|
| 1 | + #Operator Overloading in Python |
| 2 | + |
| 3 | + >>Introduction |
| 4 | + Operator overloading in Python allows you to define custom behavior for operators (such as +, -, *, and /) for your own classes. This enables you to create more intuitive and readable code when working with objects. |
| 5 | + |
| 6 | + >>What is Operator Overloading? |
| 7 | + Operator overloading is a feature in Python that allows the same operator to have different meanings based on the context, particularly the types of the operands. For example, the + operator can add two numbers, concatenate two strings, or merge two lists. |
| 8 | + |
| 9 | + >>Special Methods |
| 10 | + To overload an operator, you need to define special methods in your class. These methods are also known as magic methods or dunder methods (because they start and end with double underscores, __). |
| 11 | + |
| 12 | + Implementing Operator Overloading |
| 13 | + Here is a basic example of how to implement operator overloading in a Python class. |
| 14 | + |
| 15 | + **Example: Overloading the + Operator |
| 16 | + |
| 17 | + class Point: |
| 18 | + def __init__(self, x, y): |
| 19 | + self.x = x |
| 20 | + self.y = y |
| 21 | + |
| 22 | + def __add__(self, other): |
| 23 | + return Point(self.x + other.x, self.y + other.y) |
| 24 | + |
| 25 | + def __repr__(self): |
| 26 | + return f'Point({self.x}, {self.y})' |
| 27 | + |
| 28 | + p1 = Point(1, 2) |
| 29 | + p2 = Point(3, 4) |
| 30 | + p3 = p1 + p2 |
| 31 | + |
| 32 | + print(p3) # Output: Point(4, 6) |
| 33 | + |
| 34 | + **Examples of Operator Overloading |
| 35 | + >>Arithmetic Operators |
| 36 | + __add__(self, other): + |
| 37 | + __sub__(self, other): - |
| 38 | + __mul__(self, other): * |
| 39 | + __truediv__(self, other): / |
| 40 | + __floordiv__(self, other): // |
| 41 | + __mod__(self, other): % |
| 42 | + __pow__(self, other): ** |
| 43 | + **Example: Overloading Arithmetic Operators |
| 44 | + class Vector: |
| 45 | + def __init__(self, x, y): |
| 46 | + self.x = x |
| 47 | + self.y = y |
| 48 | + |
| 49 | + def __add__(self, other): |
| 50 | + return Vector(self.x + other.x, self.y + other.y) |
| 51 | + |
| 52 | + def __sub__(self, other): |
| 53 | + return Vector(self.x - other.x, self.y - other.y) |
| 54 | + |
| 55 | + def __mul__(self, scalar): |
| 56 | + return Vector(self.x * scalar, self.y * scalar) |
| 57 | + |
| 58 | + def __repr__(self): |
| 59 | + return f'Vector({self.x}, {self.y})' |
| 60 | + |
| 61 | + v1 = Vector(2, 3) |
| 62 | + v2 = Vector(4, 5) |
| 63 | + v3 = v1 + v2 |
| 64 | + v4 = v1 - v2 |
| 65 | + v5 = v1 * 3 |
| 66 | + |
| 67 | + print(v3) # Output: Vector(6, 8) |
| 68 | + print(v4) # Output: Vector(-2, -2) |
| 69 | + print(v5) # Output: Vector(6, 9) |
| 70 | + |
| 71 | + >>Comparison Operators |
| 72 | + __eq__(self, other): == |
| 73 | + __ne__(self, other): != |
| 74 | + __lt__(self, other): < |
| 75 | + __le__(self, other): <= |
| 76 | + __gt__(self, other): > |
| 77 | + __ge__(self, other): >= |
| 78 | + ** Example: Overloading Comparison Operators |
| 79 | + |
| 80 | + class Book: |
| 81 | + def __init__(self, title, price): |
| 82 | + self.title = title |
| 83 | + self.price = price |
| 84 | + |
| 85 | + def __eq__(self, other): |
| 86 | + return self.price == other.price |
| 87 | + |
| 88 | + def __lt__(self, other): |
| 89 | + return self.price < other.price |
| 90 | + |
| 91 | + def __repr__(self): |
| 92 | + return f'Book({self.title}, {self.price})' |
| 93 | + |
| 94 | + b1 = Book('Book A', 29.99) |
| 95 | + b2 = Book('Book B', 39.99) |
| 96 | + b3 = Book('Book C', 29.99) |
| 97 | + |
| 98 | + print(b1 == b2) # Output: False |
| 99 | + print(b1 == b3) # Output: True |
| 100 | + print(b1 < b2) # Output: True |
| 101 | + |
| 102 | + >>Other Operators |
| 103 | + |
| 104 | + __str__(self): str() |
| 105 | + __repr__(self): repr() |
| 106 | + __len__(self): len() |
| 107 | + __getitem__(self, key): indexing |
| 108 | + |
| 109 | + ** Example: Overloading __str__ and __repr__ |
| 110 | + |
| 111 | + class Person: |
| 112 | + def __init__(self, name, age): |
| 113 | + self.name = name |
| 114 | + self.age = age |
| 115 | + |
| 116 | + def __str__(self): |
| 117 | + return f'{self.name}, {self.age} years old' |
| 118 | + |
| 119 | + def __repr__(self): |
| 120 | + return f'Person(name={self.name}, age={self.age})' |
| 121 | + |
| 122 | + person = Person('Alice', 30) |
| 123 | + print(str(person)) # Output: Alice, 30 years old |
| 124 | + print(repr(person)) # Output: Person(name=Alice, age=30) |
| 125 | + |
| 126 | + >>Common Use Cases |
| 127 | + Mathematical Objects: Vectors, complex numbers, matrices, etc. |
| 128 | + Data Containers: Custom collections that need element-wise operations. |
| 129 | + Domain-Specific Objects: Objects that benefit from intuitive operations, like dates, times, and financial figures. |
| 130 | + |
| 131 | + >>Conclusion |
| 132 | + Operator overloading in Python allows you to create intuitive and readable code by defining custom behavior for operators in your own classes. By implementing special methods, you can enable your objects to interact using standard operators. |
0 commit comments