Quá tải toán tử trong python ppt

Quá tải toán tử có nghĩa là đưa ra ý nghĩa mở rộng ngoài ý nghĩa hoạt động được xác định trước của chúng. Ví dụ: toán tử + được sử dụng để cộng hai số nguyên cũng như nối hai chuỗi và hợp nhất hai danh sách. Có thể đạt được vì toán tử '+' bị quá tải bởi lớp int và lớp str. Bạn có thể nhận thấy rằng cùng một toán tử hoặc hàm tích hợp sẵn hiển thị các hành vi khác nhau cho các đối tượng thuộc các lớp khác nhau, điều này được gọi là Nạp chồng toán tử.  

Thí dụ

Python3




3
GeeksFor
3
GeeksFor
3
GeeksFor
41

3
GeeksFor
3
GeeksFor
3
GeeksFor
42

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
3
GeeksFor
3
GeeksFor
3
GeeksFor
0
3
GeeksFor
3
GeeksFor
3
GeeksFor
1
3
GeeksFor
3
GeeksFor
3
GeeksFor
2
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
4

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
3
GeeksFor
3
GeeksFor
3
GeeksFor
7
3
GeeksFor
3
GeeksFor
3
GeeksFor
1
3
GeeksFor
3
GeeksFor
3
GeeksFor
9
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

 

(3, 5)
1

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
(3, 5)
4
(3, 5)
5
(3, 5)
6
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

 

(3, 5)
8

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
3
GeeksFor
3
GeeksFor
3
GeeksFor
7
(3, 5)
5
(3, 5)
6
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

Đầu ra

3
GeeksFor
3
GeeksFor
3
GeeksFor
9

Làm cách nào để quá tải các toán tử trong Python?

Hãy xem xét rằng chúng ta có hai đối tượng là biểu diễn vật lý của một lớp (kiểu dữ liệu do người dùng định nghĩa) và chúng ta phải thêm hai đối tượng bằng toán tử '+' nhị phân, nó sẽ báo lỗi vì trình biên dịch không biết cách thêm hai đối tượng. Vì vậy, chúng tôi xác định một phương thức cho một toán tử và quá trình đó được gọi là nạp chồng toán tử. Chúng tôi có thể quá tải tất cả các toán tử hiện có nhưng chúng tôi không thể tạo một toán tử mới. Để thực hiện nạp chồng toán tử, Python cung cấp một số hàm đặc biệt hoặc hàm ma thuật được gọi tự động khi nó được liên kết với toán tử cụ thể đó. Ví dụ: khi chúng ta sử dụng toán tử +, phương thức ma thuật __add__ sẽ tự động được gọi trong đó phép toán cho toán tử + được xác định

Quá tải toán tử + nhị phân trong Python.  

Khi chúng ta sử dụng toán tử trên các kiểu dữ liệu do người dùng xác định thì tự động một hàm đặc biệt hoặc hàm ma thuật liên quan đến toán tử đó được gọi. Thay đổi hành vi của toán tử cũng đơn giản như thay đổi hành vi của một phương thức hoặc hàm. Bạn định nghĩa các phương thức trong lớp của mình và các toán tử hoạt động theo hành vi đó được xác định trong các phương thức. Khi chúng ta sử dụng toán tử +, phương thức ma thuật __add__ sẽ tự động được gọi trong đó phép toán cho toán tử + được xác định. Bằng cách thay đổi mã của phương thức kỳ diệu này, chúng ta có thể thêm ý nghĩa cho toán tử +.  

Quá tải toán tử thực sự hoạt động như thế nào?

Bất cứ khi nào bạn thay đổi hành vi của toán tử hiện có thông qua nạp chồng toán tử, bạn phải xác định lại chức năng đặc biệt được gọi tự động khi toán tử được sử dụng với các đối tượng.  

Ví dụ.  

mã 1.   

Python3




3
GeeksFor
3
GeeksFor
3
GeeksFor
05

3
GeeksFor
3
GeeksFor
3
GeeksFor
06

3
GeeksFor
3
GeeksFor
3
GeeksFor
07

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
08
3
GeeksFor
3
GeeksFor
3
GeeksFor
09

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
51
3
GeeksFor
3
GeeksFor
3
GeeksFor
52
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
54

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
53____257
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
59

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
61

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
51
3
GeeksFor
3
GeeksFor
3
GeeksFor
64
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
66

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
68
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
57
3
GeeksFor
3
GeeksFor
3
GeeksFor
1
3
GeeksFor
3
GeeksFor
3
GeeksFor
412

3
GeeksFor
3
GeeksFor
3
GeeksFor
413
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
3
GeeksFor
3
GeeksFor
3
GeeksFor
0
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
418
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
3
GeeksFor
3
GeeksFor
3
GeeksFor
2
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
423
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
3
GeeksFor
3
GeeksFor
3
GeeksFor
7
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
428
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
3
GeeksFor
3
GeeksFor
3
GeeksFor
9
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
434
3
GeeksFor
3
GeeksFor
3
GeeksFor
1
3
GeeksFor
3
GeeksFor
3
GeeksFor
436

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
438
3
GeeksFor
3
GeeksFor
3
GeeksFor
1
3
GeeksFor
3
GeeksFor
3
GeeksFor
440

3
GeeksFor
3
GeeksFor
3
GeeksFor
441

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
443

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
445

3
GeeksFor
3
GeeksFor
3
GeeksFor
446

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
448

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
00

Đầu ra

3
GeeksFor
3
GeeksFor
3
GeeksFor

Ở đây, Chúng tôi đã định nghĩa hàm đặc biệt “__add__( )”  và khi các đối tượng ob1 và ob2 được mã hóa thành “ob1 + ob2“, hàm đặc biệt sẽ tự động được gọi là ob1. __add__(ob2) có nghĩa đơn giản là ob1 gọi hàm __add__( ) với ob2 là Đối số và Nó thực sự có nghĩa là A. __add__(ob1, ob2). Do đó, khi toán tử nhị phân bị quá tải, đối tượng trước toán tử gọi hàm tương ứng với đối tượng sau toán tử làm tham số

mã 2.  

Python3




3
GeeksFor
3
GeeksFor
3
GeeksFor
01

3
GeeksFor
3
GeeksFor
3
GeeksFor
02

3
GeeksFor
3
GeeksFor
3
GeeksFor
03

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
08
3
GeeksFor
3
GeeksFor
3
GeeksFor
05
3
GeeksFor
3
GeeksFor
3
GeeksFor
06

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
51
3
GeeksFor
3
GeeksFor
3
GeeksFor
52
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
11

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
53____257
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
59

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
53____019
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
21

 

_______022____361

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
51
3
GeeksFor
3
GeeksFor
3
GeeksFor
64
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
28

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
68
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
57
3
GeeksFor
3
GeeksFor
3
GeeksFor
1
3
GeeksFor
3
GeeksFor
3
GeeksFor
34
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
19
3
GeeksFor
3
GeeksFor
3
GeeksFor
1
3
GeeksFor
3
GeeksFor
3
GeeksFor
38

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
39
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
05
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
3
GeeksFor
3
GeeksFor
3
GeeksFor
0
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
3
GeeksFor
3
GeeksFor
3
GeeksFor
2
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
47
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
05
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
3
GeeksFor
3
GeeksFor
3
GeeksFor
2
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
(3, 5)
4
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
39______01
3
GeeksFor
3
GeeksFor
3
GeeksFor
47

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
61

Đầu ra

(3, 5)

Nạp chồng toán tử so sánh trong Python.   

Python3




3
GeeksFor
3
GeeksFor
3
GeeksFor
62

3
GeeksFor
3
GeeksFor
3
GeeksFor
63

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
08
3
GeeksFor
3
GeeksFor
3
GeeksFor
09

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
51
3
GeeksFor
3
GeeksFor
3
GeeksFor
52
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
54

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
53____257
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
59

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
51
3
GeeksFor
3
GeeksFor
3
GeeksFor
78
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
28

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
82____244
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
85

3
GeeksFor
3
GeeksFor
3
GeeksFor
86____368
3
GeeksFor
3
GeeksFor
3
GeeksFor
88

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
90
3
GeeksFor
3
GeeksFor
3
GeeksFor
06

3
GeeksFor
3
GeeksFor
3
GeeksFor
86______368
3
GeeksFor
3
GeeksFor
3
GeeksFor
94

3
GeeksFor
3
GeeksFor
3
GeeksFor
413
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
3
GeeksFor
3
GeeksFor
3
GeeksFor
2
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
418
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
(3, 5)
4
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
82
(3, 5)
06

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
(3, 5)
10
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
90
3
GeeksFor
3
GeeksFor
3
GeeksFor
06

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
44
(3, 5)
17
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

đầu ra

3
GeeksFor
3
GeeksFor
3
GeeksFor
0

Quá tải đẳng thức và nhỏ hơn toán tử.  

Python3




(3, 5)
19

(3, 5)
20

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
08
3
GeeksFor
3
GeeksFor
3
GeeksFor
09

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
51
3
GeeksFor
3
GeeksFor
3
GeeksFor
52
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
54

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
53____257
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
59

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
51
(3, 5)
35
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
28

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
82____244
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
(3, 5)
42

3
GeeksFor
3
GeeksFor
3
GeeksFor
86____368
(3, 5)
45

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
90
3
GeeksFor
3
GeeksFor
3
GeeksFor
06

3
GeeksFor
3
GeeksFor
3
GeeksFor
86____368
(3, 5)
51

3
GeeksFor
3
GeeksFor
3
GeeksFor
50
3
GeeksFor
3
GeeksFor
3
GeeksFor
51
(3, 5)
54
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
28

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
82____244
3
GeeksFor
3
GeeksFor
3
GeeksFor
53
3
GeeksFor
3
GeeksFor
3
GeeksFor
57
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
(3, 5)
64

3
GeeksFor
3
GeeksFor
3
GeeksFor
86______368
(3, 5)
67

3
GeeksFor
3
GeeksFor
3
GeeksFor
55
3
GeeksFor
3
GeeksFor
3
GeeksFor
90
3
GeeksFor
3
GeeksFor
3
GeeksFor
06

3
GeeksFor
3
GeeksFor
3
GeeksFor
86____368
(3, 5)
73

(3, 5)
74

3
GeeksFor
3
GeeksFor
3
GeeksFor
413
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
3
GeeksFor
3
GeeksFor
3
GeeksFor
2
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
418
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
(3, 5)
4
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
43____986

 

3
GeeksFor
3
GeeksFor
3
GeeksFor
423
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
(3, 5)
6
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
428
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
415
(3, 5)
6
3
GeeksFor
3
GeeksFor
3
GeeksFor
3

3
GeeksFor
3
GeeksFor
3
GeeksFor
43
3
GeeksFor
3
GeeksFor
3
GeeksFor
434
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
58
3
GeeksFor
3
GeeksFor
3
GeeksFor
436

đầu ra

3
GeeksFor
3
GeeksFor
3
GeeksFor
5

Các phương thức ma thuật Python hoặc các hàm đặc biệt để nạp chồng toán tử

toán tử nhị phân

Toán tửMagic Method+__add__(self, other)–__sub__(self, other)*__mul__(self, other)/__truediv__(self, other)//__floordiv__(self, other)%__mod__(self, other)**__pow__(self . __or__(bản thân, người khác)^__xor__(bản thân, người khác)

Toán tử so sánh

OperatorMagic Method<__lt__(self, other)>__gt__(self, other)<=__le__(self, other)>=__ge__(self, other)==__eq__(self, other)!=__ne__(self, other)

Toán tử gán

Toán tửMagic Method-=__isub__(self, other)+=__iadd__(self, other)*=__imul__(self, other)/=__idiv__(self, other)//=__ifloordiv__(self, other)%=__imod__(self, other . =__ior__(bản thân, người khác)^=__ixor__(bản thân, người khác)

toán tử đơn nguyên

Toán tửMagic Method–__neg__(self)+__pos__(self)~__invert__(self)

Ghi chú. Không thể thay đổi số lượng toán hạng của một toán tử. Ví dụ. Nếu chúng ta không thể nạp chồng toán tử một ngôi thành toán tử nhị phân. Đoạn mã sau sẽ đưa ra một lỗi cú pháp

Quá tải toán tử Python là gì?

Trăn. . Quá tải toán tử có nghĩa là cung cấp ý nghĩa mở rộng ngoài ý nghĩa hoạt động được xác định trước . Ví dụ: toán tử + được sử dụng để cộng hai số nguyên cũng như nối hai chuỗi và hợp nhất hai danh sách. Có thể đạt được vì toán tử '+' bị quá tải bởi lớp int và lớp str.

Quá tải trong OOP Python là gì?

Quá tải là khả năng một hàm hoặc toán tử hành xử theo những cách khác nhau dựa trên các tham số được truyền cho hàm hoặc toán hạng mà toán tử tác động lên. Some of the advantages of using overload are: Overloading a method fosters reusability.

Ví dụ về quá tải toán tử là gì?

Nạp toán tử trong C++ . Ví dụ: chúng ta có thể nạp chồng toán tử '+' trong một lớp như Chuỗi để chúng ta có thể nối hai chuỗi chỉ bằng cách sử dụng +. C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.