Bài 36. Nạp chồng toán tử
Python Operator Overloading
You can change the meaning of an operator in Python depending upon the operands used. In this tutorial, you will learn how to use operator overloading in Python Object Oriented Programming.
Ta có thể thay đổi ý nghĩa của một toán tử trong Python tùy thuộc vào các toán hạng được sử dụng. Trong bài này ta sẽ học cách sử dụng nạp chồng toán tử trong Lập trình hướng đối tượng Python.
Nạp chồng toán tử trong Python
Python operators work for built-in classes. But the same operator behaves differently with different types. For example, the +
operator will perform arithmetic addition on two numbers, merge two lists, or concatenate two strings.
Các toán tử Python hoạt động đối với các lớp dựng sẵn. Nhưng cùng một toán tử hoạt động khác nhau với các kiểu khác nhau. Ví dụ: toán tử + sẽ thực hiện phép cộng số học trên hai số, hợp nhất hai danh sách hoặc nối hai chuỗi .
This feature in Python that allows the same operator to have different meaning according to the context is called operator overloading.
Tính năng này trong Python cho phép cùng một toán tử có nghĩa khác nhau tùy theo ngữ cảnh được gọi là nạp chồng toán tử
So what happens when we use them with objects of a user-defined class? Let us consider the following class, which tries to simulate a point in 2-D coordinate system.
Vậy điều gì sẽ xảy ra khi chúng ta sử dụng chúng với các đối tượng của một lớp do người dùng định nghĩa? Chúng ta hãy xem xét lớp sau, lớp này mô phỏng một điểm trong hệ tọa độ 2-D.
Output
Here, we can see that a TypeError
was raised, since Python didn't know how to add two Point
objects together.
However, we can achieve this task in Python through operator overloading. But first, let's get a notion about special functions.
Python Special Functions
Class functions that begin with double underscore __
are called special functions in Python.
These functions are not the typical functions that we define for a class. The __init__()
function we defined above is one of them. It gets called every time we create a new object of that class.
There are numerous other special functions in Python. Visit Python Special Functions to learn more about them.
Using special functions, we can make our class compatible with built-in functions.
Suppose we want the print()
function to print the coordinates of the Point
object instead of what we got. We can define a __str__()
method in our class that controls how the object gets printed. Let's look at how we can achieve this:
Now let's try the print()
function again.
Output
That's better. Turns out, that this same method is invoked when we use the built-in function str()
or format()
.
So, when you use str(p1)
or format(p1)
, Python internally calls the p1.__str__()
method. Hence the name, special functions.
Now let's go back to operator overloading.
Overloading the + Operator
To overload the +
operator, we will need to implement __add__()
function in the class. With great power comes great responsibility. We can do whatever we like, inside this function. But it is more sensible to return a Point
object of the coordinate sum.
Now let's try the addition operation again:
Output
What actually happens is that, when you use p1 + p2
, Python calls p1.__add__(p2)
which in turn is Point.__add__(p1,p2)
. After this, the addition operation is carried out the way we specified.
Similarly, we can overload other operators as well. The special function that we need to implement is tabulated below.
Operator | Expression | Internally |
Addition |
|
|
Subtraction |
|
|
Multiplication |
|
|
Power |
|
|
Division |
|
|
Floor Division |
|
|
Remainder (modulo) |
|
|
Bitwise Left Shift |
|
|
Bitwise Right Shift |
|
|
Bitwise AND |
|
|
Bitwise OR |
|
|
Bitwise XOR |
|
|
Bitwise NOT |
|
|
Overloading Comparison Operators
Python does not limit operator overloading to arithmetic operators only. We can overload comparison operators as well.
Suppose we wanted to implement the less than symbol <
symbol in our Point
class.
Let us compare the magnitude of these points from the origin and return the result for this purpose. It can be implemented as follows.
Output
Similarly, the special functions that we need to implement, to overload other comparison operators are tabulated below.
Operator | Expression | Internally |
Less than |
|
|
Less than or equal to |
|
|
Equal to |
|
|
Not equal to |
|
|
Greater than |
|
|
Greater than or equal to |
|
|
Last updated