Hướng dẫn python __rmul__ - python __rmul__

Đố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 30
0

Foo 30
1
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Foo 30
3
Foo 30
4
Foo 30
5

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
2
Bar 30
8
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 30
0

Foo 30
1
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Foo 30
3
Foo 30
4
Foo 30
5

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
2
Bar 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2 __rmul__ 8
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

__mul__2

Foo 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
3__mul__5__mul__6

Output:

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'

__mul__7

Foo 30
4
Bar 30
8x*y0__mul__6

x*y2x*y3x*y4 x*y5 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 30
0

Foo 30
1
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Foo 30
3
Foo 30
4
Foo 30
5

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
2
Bar 30
8
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 30
0

Foo 30
1
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Foo 30
3
Foo 30
4
Foo 30
5

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
2
Bar 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2 __rmul__ 8
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

__mul__2

Foo 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
3__mul__5__mul__6

Output:

Foo 30

__mul__7

Foo 30
4
Bar 30
8x*y0__mul__6

__rmul__

x*y2x*y3x*y4 x*y5TypeError 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 30
0

Foo 30
1
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Foo 30
3
Foo 30
4
Foo 30
5

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
2
Bar 30
8
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 30
0

Foo 30
1
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Foo 30
3
Foo 30
4
Foo 30
5

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
2
Bar 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2 __rmul__ 8
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

x*y2x*y3x*y4 x*y5

Output:

Bar 30

Trong ví dụ trên, nó giả sử f*b là

Foo 30
40 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 30
42. 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__

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 30
0

Foo 30
1
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Foo 30
3
Foo 30
4
Foo 30
5

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
2
Bar 30
8
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 30
0

Foo 30
1
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Foo 30
3
Foo 30
4
Foo 30
5

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
2
Bar 30
8
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 30
05
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9__mul__6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
6
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
7
Foo 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
5

Foo 30
1
Bar 30
2
Bar 30
3
Bar 30
4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Bar 30
6

TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
2
Bar 30
8
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 30
05
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9__mul__6

Foo 30
1
Bar 30
2
Bar 30
8
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
9
Foo 30
3x*y4
TypeError, unsupported operand type[s] for *: 'Foo' and 'Bar'
03

x*y2x*y3x*y4 x*y5

Output:

Bar [30]
Bar [30]

Bài Viết Liên Quan

Chủ Đề