Python Operator Overloading
Table of Contents
- What is operator overloading in python?
- Special functions in python
- Overloading the + operator in python
- Overloading comparison operator in python
What is an operator overloading in python?
Python users used in classrooms. However, the landlord is different from the different types. For example, the owner can not, in addition to a strong calculator with two numbers, combine the two lists and mixtures together.
This feature is located in Python, which allows operators to have different meanings in the operator.
So, what happens when I talk to the user's item defined in the class? Let's look at the next grade, which attempts to try the 2-D coordinate system.
For Example:
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
Now, give it a try and try to add two points to the terminal Python.
Yaa! What are the many complaints? TypeError may cost Python not know how to add two points to the item.
For Example:
>>> p1 = Point(2,3)
>>> p2 = Point(-1,2)
>>> p1 + p2
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'Point' and 'Point'
However, the good news is that you can learn by helping companies in Python. But first, let's go to the specific point of view.
Special function in Python
The class begins twice in __ and calls for a particular job Python. This is because, well, they are not. __Init __ () The task that I should raise is one of them. It will be called every time we create a new product. There are many specialized jobs in Python.
For Example:
>>> p1 = Point(2,3)
>>> print(p1)
<__main__.Point object at 0x00000000031F8CC0>
Through special work, we can make our classroom support for posting jobs.
It's not a good score. But if we decide __str __ () in the class, we can control the printing process. Therefore, we want to add this type of people.
For Example:
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __str__(self):
return "({0},{1})".format(self.x,self.y)
Overloading the + operator in python
For the comparison + sign, it is not necessary to apply __ __ () in the class. Great power is a great responsibility.
For Example:
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __str__(self):
return "({0},{1})".format(self.x,self.y)
def __add__(self,other):
x = self.x + other.x
y = self.y + other.y
return Point(x,y)
We can do what we like, this work. However, you are advised to go to Point Point to increase the amount.
Operator Overloading Special Functions in Python
Operator
|
Expression
|
Internally
|
Addition
|
p1 + p2
|
p1.__add__(p2)
|
Subtraction
|
p1 - p2
|
p1.__sub__(p2)
|
Multiplication
|
p1 * p2
|
p1.__mul__(p2)
|
Power
|
p1 ** p2
|
p1.__pow__(p2)
|
Division
|
p1 / p2
|
p1.__truediv__(p2)
|
Floor Division
|
p1 // p2
|
p1.__floordiv__(p2)
|
Remainder (modulo)
|
p1 % p2
|
p1.__mod__(p2)
|
Bitwise Left Shift
|
p1 << p2
|
p1.__lshift__(p2)
|
Bitwise Right Shift
|
p1 >> p2
|
p1.__rshift__(p2)
|
Bitwise AND
|
p1 & p2
|
p1.__and__(p2)
|
Bitwise OR
|
p1 | p2
|
p1.__or__(p2)
|
Bitwise XOR
|
p1 ^ p2
|
p1.__xor__(p2)
|
Bitwise NOT
|
~p1
|
p1.__invert__()
|
Overloading comparison operator in python
Python is not limited to operators that are more than arithmetic operators. It can be good for our staff.
For Example:
>>> Point(1,1) < Point(-2,-3)
True
>>> Point(1,1) < Point(0.5,-0.2)
False
>>> Point(1,1) < Point(1,1)
False
Tell them, we want to take less than the symbol 'Class Point.
I will compare the size of this site as a base and the result of this purpose. This can be done as follows.
Comparision Operator Overloading in Python
|
||
Operator
|
Expression
|
Internally
|
Less than
|
p1 < p2
|
p1.__lt__(p2)
|
Less than or equal to
|
p1 <= p2
|
p1.__le__(p2)
|
Equal to
|
p1 == p2
|
p1.__eq__(p2)
|
Not equal to
|
p1 != p2
|
p1.__ne__(p2)
|
Greater than
|
p1 > p2
|
p1.__gt__(p2)
|
Greater than or equal to
|
p1 >= p2
|
p1.__ge__(p2)
|
Comments
Post a Comment