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 GeeksFor41
3 GeeksFor 3 GeeksFor 3 GeeksFor42
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor443 GeeksFor 3 GeeksFor 3 GeeksFor0 3 GeeksFor 3 GeeksFor 3 GeeksFor1 3 GeeksFor 3 GeeksFor 3 GeeksFor23 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor4
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor443 GeeksFor 3 GeeksFor 3 GeeksFor73 GeeksFor 3 GeeksFor 3 GeeksFor13 GeeksFor 3 GeeksFor 3 GeeksFor93 GeeksFor 3 GeeksFor 3 GeeksFor3
(3, 5)1
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor44(3, 5)4 (3, 5)5 (3, 5)63 GeeksFor 3 GeeksFor 3 GeeksFor3
(3, 5)8
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor443 GeeksFor 3 GeeksFor 3 GeeksFor7(3, 5)5(3, 5)63 GeeksFor 3 GeeksFor 3 GeeksFor3
Đầu ra3 GeeksFor 3 GeeksFor 3 GeeksFor9
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 GeeksFor05
3 GeeksFor 3 GeeksFor 3 GeeksFor06
3 GeeksFor 3 GeeksFor 3 GeeksFor07
3 GeeksFor 3 GeeksFor 3 GeeksFor08 3 GeeksFor 3 GeeksFor 3 GeeksFor09
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor51 3 GeeksFor 3 GeeksFor 3 GeeksFor523 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor54
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor53____2573 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor59
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor61
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor51 3 GeeksFor 3 GeeksFor 3 GeeksFor643 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor66
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor68 3 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor573 GeeksFor 3 GeeksFor 3 GeeksFor1 3 GeeksFor 3 GeeksFor 3 GeeksFor412
3 GeeksFor 3 GeeksFor 3 GeeksFor4133 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor4153 GeeksFor 3 GeeksFor 3 GeeksFor03 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor4183 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor4153 GeeksFor 3 GeeksFor 3 GeeksFor23 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor4233 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor4153 GeeksFor 3 GeeksFor 3 GeeksFor73 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor4283 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor4153 GeeksFor 3 GeeksFor 3 GeeksFor93 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor4343 GeeksFor 3 GeeksFor 3 GeeksFor1 3 GeeksFor 3 GeeksFor 3 GeeksFor436
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor4383 GeeksFor 3 GeeksFor 3 GeeksFor1 3 GeeksFor 3 GeeksFor 3 GeeksFor440
3 GeeksFor 3 GeeksFor 3 GeeksFor441
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor443
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor445
3 GeeksFor 3 GeeksFor 3 GeeksFor446
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor448
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor00
Đầu ra3 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 GeeksFor01
3 GeeksFor 3 GeeksFor 3 GeeksFor02
3 GeeksFor 3 GeeksFor 3 GeeksFor03
3 GeeksFor 3 GeeksFor 3 GeeksFor08 3 GeeksFor 3 GeeksFor 3 GeeksFor053 GeeksFor 3 GeeksFor 3 GeeksFor06
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor51 3 GeeksFor 3 GeeksFor 3 GeeksFor523 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor11
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor53____2573 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor59
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor53____0193 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor21
_______022____361
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor51 3 GeeksFor 3 GeeksFor 3 GeeksFor643 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor28
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor68 3 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor573 GeeksFor 3 GeeksFor 3 GeeksFor1 3 GeeksFor 3 GeeksFor 3 GeeksFor343 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor193 GeeksFor 3 GeeksFor 3 GeeksFor1 3 GeeksFor 3 GeeksFor 3 GeeksFor38
3 GeeksFor 3 GeeksFor 3 GeeksFor393 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor053 GeeksFor 3 GeeksFor 3 GeeksFor443 GeeksFor 3 GeeksFor 3 GeeksFor03 GeeksFor 3 GeeksFor 3 GeeksFor443 GeeksFor 3 GeeksFor 3 GeeksFor23 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor473 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor053 GeeksFor 3 GeeksFor 3 GeeksFor443 GeeksFor 3 GeeksFor 3 GeeksFor23 GeeksFor 3 GeeksFor 3 GeeksFor44(3, 5)43 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor39______01 3 GeeksFor 3 GeeksFor 3 GeeksFor47
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor61
Đầu ra(3, 5)
Nạp chồng toán tử so sánh trong Python.
Python3
3 GeeksFor 3 GeeksFor 3 GeeksFor62
3 GeeksFor 3 GeeksFor 3 GeeksFor63
3 GeeksFor 3 GeeksFor 3 GeeksFor08 3 GeeksFor 3 GeeksFor 3 GeeksFor09
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor51 3 GeeksFor 3 GeeksFor 3 GeeksFor523 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor54
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor53____2573 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor59
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor51 3 GeeksFor 3 GeeksFor 3 GeeksFor783 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor28
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor82____2443 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor85
3 GeeksFor 3 GeeksFor 3 GeeksFor86____368 3 GeeksFor 3 GeeksFor 3 GeeksFor88
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor903 GeeksFor 3 GeeksFor 3 GeeksFor06
3 GeeksFor 3 GeeksFor 3 GeeksFor86______368 3 GeeksFor 3 GeeksFor 3 GeeksFor94
3 GeeksFor 3 GeeksFor 3 GeeksFor4133 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor4153 GeeksFor 3 GeeksFor 3 GeeksFor23 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor4183 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor415(3, 5)43 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor82(3, 5)06
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor44(3, 5)103 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor903 GeeksFor 3 GeeksFor 3 GeeksFor06
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor44(3, 5)173 GeeksFor 3 GeeksFor 3 GeeksFor3
đầu ra
3 GeeksFor 3 GeeksFor 3 GeeksFor0Quá tải đẳng thức và nhỏ hơn toán tử.
Python3
(3, 5)19
(3, 5)20
3 GeeksFor 3 GeeksFor 3 GeeksFor08 3 GeeksFor 3 GeeksFor 3 GeeksFor09
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor51 3 GeeksFor 3 GeeksFor 3 GeeksFor523 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor54
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor53____2573 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor59
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor51 (3, 5)353 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor28
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor82____2443 GeeksFor 3 GeeksFor 3 GeeksFor53(3, 5)42
3 GeeksFor 3 GeeksFor 3 GeeksFor86____368 (3, 5)45
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor903 GeeksFor 3 GeeksFor 3 GeeksFor06
3 GeeksFor 3 GeeksFor 3 GeeksFor86____368 (3, 5)51
3 GeeksFor 3 GeeksFor 3 GeeksFor503 GeeksFor 3 GeeksFor 3 GeeksFor51 (3, 5)543 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor28
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor82____2443 GeeksFor 3 GeeksFor 3 GeeksFor533 GeeksFor 3 GeeksFor 3 GeeksFor573 GeeksFor 3 GeeksFor 3 GeeksFor583 GeeksFor 3 GeeksFor 3 GeeksFor58 (3, 5)64
3 GeeksFor 3 GeeksFor 3 GeeksFor86______368 (3, 5)67
3 GeeksFor 3 GeeksFor 3 GeeksFor553 GeeksFor 3 GeeksFor 3 GeeksFor903 GeeksFor 3 GeeksFor 3 GeeksFor06
3 GeeksFor 3 GeeksFor 3 GeeksFor86____368 (3, 5)73
(3, 5)74
3 GeeksFor 3 GeeksFor 3 GeeksFor4133 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor4153 GeeksFor 3 GeeksFor 3 GeeksFor23 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor4183 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor415(3, 5)43 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor43____986
3 GeeksFor 3 GeeksFor 3 GeeksFor4233 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor415(3, 5)63 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor4283 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor415(3, 5)63 GeeksFor 3 GeeksFor 3 GeeksFor3
3 GeeksFor 3 GeeksFor 3 GeeksFor433 GeeksFor 3 GeeksFor 3 GeeksFor4343 GeeksFor 3 GeeksFor 3 GeeksFor583 GeeksFor 3 GeeksFor 3 GeeksFor58 3 GeeksFor 3 GeeksFor 3 GeeksFor436
đầu ra
3 GeeksFor 3 GeeksFor 3 GeeksFor5Cá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__gt__(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