Đối với mỗi dấu hiệu vận hành, có một cơ chế cơ bản. Cơ chế cơ bản này là một phương pháp đặc biệt sẽ được gọi trong quá trình hành động của nhà điều hành. Phương pháp đặc biệt này được gọi là phương pháp ma thuật. Đối với mọi tính toán số học như +, -, *, /, chúng tôi yêu cầu 2 toán hạng để thực hiện chức năng toán tử.magical method. For every arithmetic calculation like +, -, *, /, we require 2 operands to carry out operator functionality.
Examples:
‘+’ ? ‘__add__’ method ‘_’ ? ‘__sub__’ method ‘*’ ? ‘__mul__’ method
Vì bài viết được giới hạn trong chức năng nhân, chúng ta sẽ thấy về quy trình nhân tại đây. Để thực hiện chức năng nhân, chúng ta phải buộc dấu hiệu toán tử với toán hạng bên trái/phải. Trước đây, đi đến __rmul__
Method, chúng ta sẽ thấy về phương pháp __mul__
, giúp chúng ta hiểu chức năng nhân một cách sống động.
__mul__[]
Chúng ta hãy lấy biểu thức x*y
trong đó x là một thể hiện của lớp A. Để thực hiện phương thức __mul__
, toán tử nhìn vào lớp của toán hạng bên trái [x] để hiện tại __mul__ tức là toán tử [*] sẽ kiểm tra lớp A cho sự hiện diện của phương pháp '__mul__
' trong đó. Nếu nó có phương thức __mul__
, nó sẽ gọi
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'1. Mặt khác, nó ném thông báo lỗi ‘typeerror: không được hỗ trợ.TypeError: unsupported operands’ error message.
Ví dụ 1:
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'3
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 300
Foo 301
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 303
Foo 304
Foo 305
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 300
Foo 301
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 303
Foo 304
Foo 305
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
__rmul__
8 Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
__mul__
2
Foo 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'3
__mul__
5__mul__
6Output:
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
__mul__
7
Foo 304
Bar 308
x*y
0__mul__
6x*y
2x*y
3x*y
4 x*y
5 Lets add the __mul__ method in Foo class.
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'3
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 300
Foo 301
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 303
Foo 304
Foo 305
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 300
Foo 301
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 303
Foo 304
Foo 305
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
__rmul__
8 Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
__mul__
2
Foo 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'3
__mul__
5__mul__
6Output:
Foo 30
__mul__
7
Foo 304
Bar 308
x*y
0__mul__
6__rmul__
x*y
2x*y
3x*y
4 x*y
5TypeError error
message
Trong ví dụ trên, toán hạng đầu tiên là F và lớp foo [] của nó. Vì foo [] không có phương thức __mul__
, nó không hiểu cách nhân. Vì vậy, nó sẽ hiển thị thông báo kiểu loại. Nếu chúng tôi kiểm tra thanh lớp khác [], thậm chí nó không có phương thức __mul__
. Vì vậy, ngay cả khi chúng ta đảo ngược phép nhân thành [b*f], nó sẽ ném cùng một lỗi Let’s take the above example with a small modification.
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'3
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 300
Foo 301
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 303
Foo 304
Foo 305
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 300
Foo 301
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 303
Foo 304
Foo 305
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
__rmul__
8 Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
x*y
2x*y
3x*y
4 x*y
5
Output:
Bar 30
Trong ví dụ trên, nó giả sử f*b là
Foo 3040 là phương thức
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'59 có mặt trong lớp thanh [] của phiên bản b. Nếu chúng ta đảo ngược sự nhân lên [b*f]. Ký hiệu sẽ là
Foo 3042. Nếu nó không có phương pháp
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'59, thì nó có thể hiểu những gì cần ghi nhận và ném tin nhắn kiểu mẫu.
Các loại toán tử này, yêu cầu 2 toán hạng, theo mặc định sẽ mang cả phương thức __mul__
và
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'59. Để thực hiện phép nhân với cả phép nhân bình thường và ngược, hãy xem ví dụ dưới đây.
Ví dụ 2:
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'3
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 300
Foo 301
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 303
Foo 304
Foo 305
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 300
Foo 301
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 303
Foo 304
Foo 305
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 3005
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
__mul__
6TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
Foo 301
Bar 302
Bar 303
Bar 304
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Bar 306
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'2
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'5
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'7
Foo 3005
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
__mul__
6Foo 301
Bar 302
Bar 308
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'9
Foo 303
x*y
4 TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'03
x*y
2x*y
3x*y
4 x*y
5
Output:
Bar [30] Bar [30]