Nếu bạn đã sử dụng toán tử
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
6 hoặc >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
7 trên đối tượng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
8 trong Python, bạn hẳn đã nhận thấy hành vi khác của nó khi so sánh với đối tượng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
9 hoặc >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
0>>>
>>> # Adds the two numbers
>>> 1 + 2
3
>>> # Concatenates the two strings
>>> 'Real' + 'Python'
'RealPython'
>>> # Gives the product
>>> 3 * 2
6
>>> # Repeats the string
>>> 'Python' * 3
'PythonPythonPython'
Bạn có thể thắc mắc làm thế nào mà cùng một toán tử hoặc hàm dựng sẵn lại hiển thị các hành vi khác nhau đối với 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ử hoặc nạp chồng hàm tương ứng. Bài viết này sẽ giúp bạn hiểu cơ chế này, để bạn có thể thực hiện tương tự trong các lớp Python của riêng mình và làm cho các đối tượng của bạn giống Pythonic hơn
Bạn sẽ học những điều sau đây
- API xử lý toán tử và tích hợp sẵn trong Python
- “Bí mật” đằng sau
1 và các phần mềm tích hợp khác>>> class Order: .. def __init__[self, cart, customer]: .. self.cart = list[cart] .. self.customer = customer ... >>> order = Order[['banana', 'apple', 'mango'], 'Real Python'] >>> len[order] # Calling len when no __len__ Traceback [most recent call last]: File "", line 1, in TypeError: object of type 'Order' has no len[]
- Cách làm cho các lớp của bạn có khả năng sử dụng các toán tử
- Cách làm cho các lớp của bạn tương thích với các hàm dựng sẵn của Python
Tiền thưởng miễn phí. Nhấp vào đây để có quyền truy cập vào Bảng cheat Python OOP miễn phí chỉ cho bạn các hướng dẫn, video và sách hay nhất để tìm hiểu thêm về Lập trình hướng đối tượng với Python
Ngoài ra, bạn cũng sẽ thấy một lớp ví dụ, các đối tượng của lớp này sẽ tương thích với nhiều toán tử và hàm này. Bắt đầu nào
Mô hình dữ liệu Python
Giả sử bạn có một lớp đại diện cho một đơn đặt hàng trực tuyến có một giỏ hàng [một
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
2] và một khách hàng [một >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
8 hoặc phiên bản của một lớp khác đại diện cho một khách hàng]Ghi chú. Nếu bạn cần xem lại OOP trong Python, hãy xem hướng dẫn này trên Real Python. Lập trình hướng đối tượng [OOP] trong Python 3
Trong trường hợp như vậy, việc muốn có được độ dài của danh sách giỏ hàng là điều hoàn toàn tự nhiên. Một người mới sử dụng Python có thể quyết định triển khai một phương thức có tên là
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
4 trong lớp của họ để thực hiện việc này. Nhưng bạn có thể định cấu hình >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1 tích hợp theo cách nó trả về độ dài của danh sách giỏ hàng khi được cung cấp đối tượng của chúng taTrong một trường hợp khác, chúng tôi có thể muốn thêm thứ gì đó vào giỏ hàng. Một lần nữa, một người mới sử dụng Python sẽ nghĩ đến việc triển khai một phương thức có tên là
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
6 lấy một mặt hàng và thêm nó vào danh sách giỏ hàng. Nhưng bạn có thể định cấu hình toán tử >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
6 theo cách nó thêm một mặt hàng mới vào giỏ hàngPython thực hiện tất cả điều này bằng các phương thức đặc biệt. Các phương thức đặc biệt này có quy ước đặt tên, trong đó tên bắt đầu bằng hai dấu gạch dưới, theo sau là một mã định danh và kết thúc bằng một cặp dấu gạch dưới khác
Về cơ bản, mỗi hàm hoặc toán tử tích hợp có một phương thức đặc biệt tương ứng với nó. Ví dụ: có
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
8 tương ứng với >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50 tương ứng với toán tử >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
6Theo mặc định, hầu hết các toán tử và tích hợp sẵn sẽ không hoạt động với các đối tượng trong lớp của bạn. Bạn phải thêm các phương thức đặc biệt tương ứng trong định nghĩa lớp của mình để làm cho đối tượng của bạn tương thích với các toán tử và tích hợp sẵn
Khi bạn thực hiện điều này, hành vi của hàm hoặc toán tử được liên kết với nó sẽ thay đổi theo hành vi được xác định trong phương thức
Đây chính là điều mà Mô hình dữ liệu [Phần 3 của tài liệu Python] giúp bạn hoàn thành. Nó liệt kê tất cả các phương thức đặc biệt có sẵn và cung cấp cho bạn phương tiện nạp chồng các hàm và toán tử dựng sẵn để bạn có thể sử dụng chúng trên các đối tượng của riêng mình
Hãy xem điều này có nghĩa là gì
Sự thật thú vị. Do quy ước đặt tên được sử dụng cho các phương thức này, chúng còn được gọi là phương thức dunder, viết tắt của phương thức gạch dưới kép. Đôi khi chúng còn được gọi là phương pháp đặc biệt hoặc phương pháp ma thuật. Mặc dù vậy, chúng tôi thích các phương pháp dunder hơn
Loại bỏ các quảng cáoNội bộ của các hoạt động như >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
53
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
Mỗi lớp trong Python định nghĩa hành vi riêng của nó cho các hàm và phương thức tích hợp. Khi bạn chuyển một thể hiện của lớp nào đó cho một hàm dựng sẵn hoặc sử dụng một toán tử trên thể hiện đó, nó thực sự tương đương với việc gọi một phương thức đặc biệt với các đối số liên quan
Nếu có một hàm tích hợp,
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
54 và phương thức đặc biệt tương ứng cho hàm là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
55, thì Python diễn giải lời gọi hàm là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
56, trong đó >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
57 là đối tượng. Trong trường hợp toán tử, nếu bạn có toán tử >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
58 và phương thức đặc biệt tương ứng cho nó là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
59, thì Python diễn giải thứ gì đó như >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50 thành >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
51Vì vậy, khi bạn đang gọi
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1 trên một đối tượng, Python sẽ xử lý cuộc gọi là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
53. Khi bạn sử dụng toán tử >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
53 trên một iterable để lấy giá trị tại một chỉ mục, Python sẽ xử lý nó dưới dạng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
55, trong đó >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
56 là đối tượng có thể lặp lại và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
57 là chỉ mục bạn muốn lấyDo đó, khi bạn định nghĩa các phương thức đặc biệt này trong lớp của riêng mình, bạn sẽ ghi đè hành vi của hàm hoặc toán tử được liên kết với chúng bởi vì, đằng sau hậu trường, Python đang gọi phương thức của bạn. Hãy hiểu rõ hơn về điều này
>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
3Như bạn có thể thấy, khi bạn sử dụng hàm hoặc phương thức đặc biệt tương ứng của nó, bạn sẽ nhận được kết quả tương tự. Trên thực tế, khi bạn lấy danh sách các thuộc tính và phương thức của một đối tượng
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
8 bằng cách sử dụng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
59, bạn sẽ thấy các phương thức đặc biệt này trong danh sách bên cạnh các phương thức thông thường có sẵn trên các đối tượng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
8>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
7Nếu hành vi của một hàm hoặc toán tử tích hợp sẵn không được định nghĩa trong lớp bằng phương thức đặc biệt, thì bạn sẽ nhận được một
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
81Vì vậy, làm thế nào bạn có thể sử dụng các phương pháp đặc biệt trong các lớp học của mình?
Quá tải chức năng tích hợp
Nhiều phương pháp đặc biệt được xác định trong Mô hình Dữ liệu có thể được sử dụng để thay đổi hành vi của các hàm, chẳng hạn như
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
82, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
83, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
84, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
85, v.v. Để làm điều này, bạn chỉ cần định nghĩa phương thức đặc biệt tương ứng trong lớp của mình. Hãy xem xét một vài ví dụCung cấp độ dài cho các đối tượng của bạn bằng cách sử dụng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
Để thay đổi hành vi của
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1, bạn cần xác định phương thức đặc biệt của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
88 trong lớp của mình. Bất cứ khi nào bạn chuyển một đối tượng trong lớp của mình tới >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1, định nghĩa tùy chỉnh của bạn về >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
88 sẽ được sử dụng để lấy kết quả. Hãy triển khai >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1 cho lớp đặt hàng mà chúng ta đã nói ở phần đầu>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
Như bạn có thể thấy, bây giờ bạn có thể sử dụng
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1 để lấy trực tiếp chiều dài của giỏ hàng. Hơn nữa, sẽ có ý nghĩa trực quan hơn khi nói "độ dài của đơn đặt hàng" thay vì gọi một cái gì đó như >>> # Adds the two numbers
>>> 1 + 2
3
>>> # Concatenates the two strings
>>> 'Real' + 'Python'
'RealPython'
>>> # Gives the product
>>> 3 * 2
6
>>> # Repeats the string
>>> 'Python' * 3
'PythonPythonPython'
13. Cuộc gọi của bạn vừa Pythonic vừa trực quan hơn. Khi bạn không xác định phương thức >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
88 nhưng vẫn gọi >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1 trên đối tượng của mình, bạn sẽ nhận được một >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
81>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
Tuy nhiên, khi quá tải
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1, bạn nên nhớ rằng Python yêu cầu hàm trả về một số nguyên. Nếu phương thức của bạn trả về bất kỳ giá trị nào khác ngoài số nguyên, bạn sẽ nhận được _____381. Điều này, rất có thể, là để giữ cho nó phù hợp với thực tế là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1 thường được sử dụng để lấy độ dài của một chuỗi, mà chỉ có thể là một số nguyên>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
5Làm cho các đối tượng của bạn hoạt động với >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
00
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
Bạn có thể ra lệnh cho hành vi của
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
00 tích hợp sẵn cho các phiên bản của lớp của bạn bằng cách xác định phương thức đặc biệt của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
02 trong lớp. Không có giới hạn nào đối với giá trị trả về của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
00 và bạn nhận được một >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
81 khi phương thức đặc biệt không có trong định nghĩa lớp của bạnTrong một lớp biểu diễn một vectơ trong không gian hai chiều, có thể sử dụng
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
00 để lấy độ dài của vectơ. Hãy xem nó hoạt động>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
5Sẽ có ý nghĩa trực quan hơn khi nói "giá trị tuyệt đối của vectơ" thay vì gọi một cái gì đó như
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
06Loại bỏ các quảng cáoIn các đối tượng của bạn đẹp mắt bằng cách sử dụng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
07
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
Tích hợp sẵn
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
07 được sử dụng để truyền một thể hiện của một lớp thành một đối tượng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
8, hoặc thích hợp hơn, để có được một biểu diễn chuỗi thân thiện với người dùng của đối tượng mà người dùng bình thường có thể đọc được chứ không phải lập trình viên. Bạn có thể xác định định dạng chuỗi mà đối tượng của bạn sẽ được hiển thị khi được chuyển đến >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
07 bằng cách xác định phương thức >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
301 trong lớp của bạn. Hơn nữa, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
301 là phương thức được Python sử dụng khi bạn gọi >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
303 trên đối tượng của mìnhHãy triển khai điều này trong lớp
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
304 để định dạng các đối tượng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
304 thành >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
306. Thành phần y âm sẽ được xử lý bằng ngôn ngữ mini định dạng>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
8Điều cần thiết là
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
301 trả về một đối tượng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
8 và chúng tôi nhận được một >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
81 nếu kiểu trả về không phải là chuỗiĐại diện cho các đối tượng của bạn bằng cách sử dụng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
310
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
Tích hợp sẵn
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
310 được sử dụng để lấy biểu diễn chuỗi có thể phân tích cú pháp của một đối tượng. Nếu một đối tượng có thể phân tích cú pháp, điều đó có nghĩa là Python sẽ có thể tạo lại đối tượng từ biểu diễn khi >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
312 được sử dụng cùng với các hàm như >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
313. Để xác định hành vi của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
310, bạn có thể sử dụng phương thức đặc biệt của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
315Đây cũng là phương thức Python sử dụng để hiển thị đối tượng trong phiên REPL. Nếu phương thức
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
315 không được xác định, bạn sẽ nhận được một cái gì đó giống như >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
317 khi cố gắng xem xét đối tượng trong phiên REPL. Hãy xem nó hoạt động trong lớp >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
304>>>
>>> # Adds the two numbers
>>> 1 + 2
3
>>> # Concatenates the two strings
>>> 'Real' + 'Python'
'RealPython'
>>> # Gives the product
>>> 3 * 2
6
>>> # Repeats the string
>>> 'Python' * 3
'PythonPythonPython'
1Ghi chú. Trong trường hợp phương thức
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
301 không được xác định, Python sử dụng phương thức >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
315 để in đối tượng, cũng như để biểu diễn đối tượng khi >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
07 được gọi trên đó. Nếu thiếu cả hai phương thức, nó sẽ mặc định là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
322. Nhưng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
315 là phương pháp duy nhất được sử dụng để hiển thị đối tượng trong phiên tương tác. Sự vắng mặt của nó trong lớp mang lại >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
322Ngoài ra, trong khi sự khác biệt này giữa
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
301 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
315 là hành vi được khuyến nghị, nhiều thư viện phổ biến bỏ qua sự khác biệt này và sử dụng hai phương pháp thay thế cho nhauĐây là một bài báo được đề xuất trên
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
315 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
301 bởi Dan Bader của chính chúng tôi. Chuyển đổi chuỗi Python 101. Tại sao mọi lớp học đều cần một "đại diện"Làm cho các đối tượng của bạn thành thật hoặc giả bằng cách sử dụng ________ 3329
Tích hợp sẵn
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
329 có thể được sử dụng để lấy giá trị thực của một đối tượng. Để xác định hành vi của nó, bạn có thể sử dụng hàm >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
331 [______3332 trong Python 2. x] phương pháp đặc biệtHành vi được xác định ở đây sẽ xác định giá trị thực của một thể hiện trong tất cả các ngữ cảnh yêu cầu lấy giá trị thực, chẳng hạn như trong các câu lệnh
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
333Ví dụ, đối với lớp
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
334 đã được định nghĩa ở trên, một thể hiện có thể được coi là đúng nếu độ dài của danh sách giỏ hàng khác không. Điều này có thể được sử dụng để kiểm tra xem một đơn đặt hàng có nên được xử lý hay không>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
0Ghi chú. Khi phương pháp đặc biệt
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
331 không được triển khai trong một lớp, giá trị trả về bởi >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
88 được sử dụng làm giá trị thực, trong đó giá trị khác 0 biểu thị >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
337 và giá trị 0 biểu thị >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
338. Trong trường hợp cả hai phương thức không được triển khai, tất cả các thể hiện của lớp được coi là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
337Có nhiều phương thức đặc biệt hơn làm quá tải các hàm tích hợp. Bạn có thể tìm thấy chúng trong tài liệu. Đã thảo luận về một số trong số họ, hãy chuyển sang các toán tử
Quá tải các toán tử tích hợp
Thay đổi hành vi của toán tử cũng đơn giản như thay đổi hành vi của hàm. Bạn định nghĩa các phương thức đặc biệt tương ứng của chúng 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 này
Chúng khác với các phương thức đặc biệt ở trên theo nghĩa là chúng cần chấp nhận một đối số khác trong định nghĩa ngoài
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
340, thường được gọi bằng tên >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
341. Hãy xem xét một vài ví dụLoại bỏ các quảng cáoLàm cho các đối tượng của bạn có khả năng được thêm vào bằng cách sử dụng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
6
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
Phương thức đặc biệt tương ứng với toán tử
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
6 là phương thức >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50. Việc thêm định nghĩa tùy chỉnh của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50 sẽ thay đổi hành vi của toán tử. Khuyến nghị rằng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50 trả về một thể hiện mới của lớp thay vì sửa đổi chính thể hiện đang gọi. Bạn sẽ thấy hành vi này khá phổ biến trong Python>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
30Bạn có thể thấy ở trên rằng việc sử dụng toán tử
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
6 trên đối tượng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
8 thực sự trả về một phiên bản >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
8 mới, giữ nguyên giá trị của phiên bản đang gọi [>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
350]. Để thay đổi nó, chúng ta cần chỉ định rõ ràng phiên bản mới cho >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
350Hãy triển khai khả năng thêm các mặt hàng mới vào giỏ hàng của chúng ta trong lớp
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
334 bằng cách sử dụng toán tử. Chúng tôi sẽ làm theo phương pháp được đề xuất và làm cho toán tử trả về một phiên bản >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
334 mới có các thay đổi theo yêu cầu của chúng tôi thay vì thực hiện các thay đổi trực tiếp với phiên bản của chúng tôi>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
31Tương tự, bạn có
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
354, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
355 và các phương thức đặc biệt khác xác định hành vi của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
356, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
7, v.v. Các phương thức này cũng sẽ trả về một thể hiện mới của lớpphím tắt. nhà điều hành >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
358
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
Toán tử
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
358 là viết tắt của biểu thức >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
360. Phương thức đặc biệt tương ứng với nó là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
361. Phương thức >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
361 sẽ thực hiện các thay đổi trực tiếp đối với đối số >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
340 và trả về kết quả, có thể hoặc không thể là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
340. Hành vi này khá khác so với >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50 vì cái sau tạo một đối tượng mới và trả về đối tượng đó, như bạn đã thấy ở trênĐại khái, bất kỳ việc sử dụng
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
358 nào trên hai đối tượng đều tương đương với điều này>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
32Ở đây,
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
367 là giá trị được trả về bởi >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
361. Phép gán thứ hai được Python tự động xử lý, nghĩa là bạn không cần gán rõ ràng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
369 cho kết quả như trong trường hợp của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
360Hãy biến điều này thành có thể đối với lớp
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
334 để các mặt hàng mới có thể được thêm vào giỏ hàng bằng cách sử dụng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
358>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
33Như có thể thấy, bất kỳ thay đổi nào được thực hiện trực tiếp tới
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
340 và sau đó được trả lại. Điều gì xảy ra khi bạn trả về một số giá trị ngẫu nhiên, chẳng hạn như một chuỗi hoặc một số nguyên?>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
34Mặc dù mặt hàng có liên quan đã được thêm vào giỏ hàng, nhưng giá trị của
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
374 đã thay đổi thành giá trị được trả về bởi >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
361. Python đã ngầm xử lý bài tập cho bạn. Điều này có thể dẫn đến hành vi đáng ngạc nhiên nếu bạn quên trả lại thứ gì đó trong quá trình triển khai của mình>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
35Vì tất cả các hàm [hoặc phương thức] Python hoàn toàn trả về
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
376, nên >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
374 được gán lại cho >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
376 và phiên REPL không hiển thị bất kỳ đầu ra nào khi kiểm tra >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
374. Nhìn vào loại >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
374, bạn thấy rằng bây giờ là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
381. Do đó, hãy luôn đảm bảo rằng bạn đang trả lại thứ gì đó khi triển khai >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
361 và đó là kết quả của hoạt động chứ không phải bất kỳ thứ gì khácTương tự như
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
361, bạn có >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
384, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
385, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
386 và các phương thức đặc biệt khác xác định hành vi của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
387, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
388, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
389 và các phương thức khác tương tựGhi chú. Khi
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
361 hoặc các bạn của nó bị thiếu trong định nghĩa lớp của bạn nhưng bạn vẫn sử dụng các toán tử của chúng trên các đối tượng của mình, Python sử dụng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50 và các bạn của nó để lấy kết quả của phép toán và gán kết quả đó cho thể hiện gọi. Nói chung, sẽ an toàn nếu không triển khai >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
361 và các bạn bè của nó trong lớp học của bạn miễn là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50 và các bạn bè của nó hoạt động bình thường [trả lại thứ gì đó là kết quả của hoạt động]Tài liệu Python có một lời giải thích tốt về các phương pháp này. Ngoài ra, hãy xem ví dụ này cho thấy những cảnh báo liên quan đến
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
358 và những thứ khác khi làm việc với các loại không thay đổiLoại bỏ các quảng cáoLập chỉ mục và cắt các đối tượng của bạn bằng cách sử dụng >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
53
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
Toán tử
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
53 được gọi là toán tử lập chỉ mục và được sử dụng trong nhiều ngữ cảnh khác nhau trong Python, chẳng hạn như lấy giá trị tại một chỉ mục theo trình tự, lấy giá trị được liên kết với một khóa trong từ điển hoặc lấy một phần của chuỗi thông qua việc cắt. Bạn có thể thay đổi hành vi của nó bằng phương pháp đặc biệt >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
397Hãy định cấu hình lớp
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
334 của chúng tôi để chúng tôi có thể trực tiếp sử dụng đối tượng và lấy một mặt hàng từ giỏ hàng>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
36Bạn sẽ nhận thấy rằng ở trên, tên của đối số cho
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
397 không phải là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
57 mà là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
701. Điều này là do đối số có thể có ba hình thức chủ yếu. một giá trị số nguyên, trong trường hợp đó là chỉ mục hoặc khóa từ điển, giá trị chuỗi, trong trường hợp đó là khóa từ điển và đối tượng lát, trong trường hợp đó, nó sẽ cắt chuỗi được sử dụng bởi lớp. Mặc dù có những khả năng khác, nhưng đây là những khả năng thường gặp nhấtVì cấu trúc dữ liệu bên trong của chúng ta là một danh sách nên chúng ta có thể sử dụng toán tử
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
53 để cắt danh sách, như trong trường hợp này, đối số >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
701 sẽ là một đối tượng lát. Đây là một trong những lợi thế lớn nhất của việc có một định nghĩa >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
397 trong lớp học của bạn. Miễn là bạn đang sử dụng cấu trúc dữ liệu hỗ trợ cắt [danh sách, bộ dữ liệu, chuỗi, v.v.], bạn có thể định cấu hình các đối tượng của mình để trực tiếp cắt cấu trúc>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
37Ghi chú. Có một phương pháp đặc biệt tương tự
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
705 được sử dụng để xác định hành vi của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
706. Phương thức này nhận thêm hai đối số ngoài >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
340, thường được gọi là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
701 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
709, và có thể được sử dụng để thay đổi giá trị tại >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
701 thành >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
709toán tử đảo ngược. Làm cho lớp học của bạn chính xác về mặt toán học
Trong khi định nghĩa các phương thức đặc biệt
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
354, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
355 và tương tự cho phép bạn sử dụng các toán tử khi đối tượng lớp của bạn là toán hạng bên trái, toán tử sẽ không hoạt động nếu đối tượng lớp là toán hạng bên phải>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
38Nếu lớp của bạn đại diện cho một thực thể toán học như vectơ, tọa độ hoặc số phức, thì việc áp dụng các toán tử sẽ hoạt động trong cả hai trường hợp vì đây là một phép toán hợp lệ
Hơn nữa, nếu các toán tử chỉ hoạt động khi thể hiện là toán hạng bên trái, chúng ta đang vi phạm nguyên tắc cơ bản của tính giao hoán trong nhiều trường hợp. Do đó, để giúp bạn làm cho các lớp của mình chính xác về mặt toán học, Python cung cấp cho bạn các phương thức đảo ngược đặc biệt như
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
715, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
716, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
717, v.v.Các cuộc gọi xử lý này chẳng hạn như
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
718, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
719 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
720, trong đó >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
721 không phải là một thể hiện của lớp liên quan. Cũng giống như >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50 và các phương thức khác, các phương thức đảo ngược đặc biệt này sẽ trả về một thể hiện mới của lớp với các thay đổi của thao tác thay vì sửa đổi chính thể hiện đang gọiHãy định cấu hình
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
715 trong lớp >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
334 theo cách nó sẽ thêm một thứ gì đó ở phía trước giỏ hàng. Điều này có thể được sử dụng trong trường hợp giỏ hàng được sắp xếp theo mức độ ưu tiên của các đơn đặt hàng>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
39Một ví dụ hoàn chỉnh
Để đưa tất cả những điểm này về nhà, tốt hơn là xem xét một lớp ví dụ triển khai các toán tử này cùng nhau
Hãy phát minh lại bánh xe và triển khai lớp của riêng chúng ta để biểu diễn các số phức,
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
725. Các đối tượng của lớp chúng ta sẽ hỗ trợ nhiều hàm và toán tử dựng sẵn, làm cho chúng hoạt động rất giống với lớp số phức dựng sẵn>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
70Hàm tạo chỉ xử lý một loại cuộc gọi,
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
726. Nó nhận các đối số vị trí, biểu diễn phần thực và phần ảo của số phứcHãy xác định hai phương thức bên trong lớp,
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
727 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
728, sẽ cung cấp cho chúng ta liên hợp phức và đối số của một số phức tương ứng>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
71Ghi chú.
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
729 không phải là một phương thức đặc biệt mà là một thuộc tính lớp được mặc định. Nó có một tham chiếu đến lớp. Bằng cách sử dụng nó ở đây, chúng tôi có được điều đó và sau đó gọi hàm tạo theo cách thông thường. Nói cách khác, điều này tương đương với >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
730. Điều này được thực hiện ở đây để tránh cấu trúc lại mã nếu tên của lớp thay đổi vào một ngày nào đóTiếp theo, chúng tôi định cấu hình
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
00 để trả về mô đun của một số phức>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
72Chúng tôi sẽ tuân theo sự phân biệt được khuyến nghị giữa
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
315 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
301 và sử dụng cách đầu tiên để biểu diễn chuỗi có thể phân tích cú pháp và cách thứ hai để biểu diễn "đẹp"Phương thức
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
315 sẽ chỉ trả về >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
726 trong một chuỗi để chúng ta có thể gọi >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
313 để tạo lại đối tượng, trong khi phương thức >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
301 sẽ trả về số phức trong ngoặc đơn, như >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
738>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
73Về mặt toán học, có thể cộng hai số phức bất kỳ hoặc cộng một số thực thành một số phức. Hãy định cấu hình toán tử
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
6 sao cho nó hoạt động cho cả hai trường hợpPhương thức sẽ kiểm tra loại toán tử bên tay phải. Trong trường hợp là một số
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
9 hoặc một số >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
0, nó sẽ chỉ tăng phần thực [vì bất kỳ số thực nào, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
350, đều tương đương với >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
743], trong khi đối với một số phức khác, nó sẽ thay đổi cả hai phần>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
74Tương tự, chúng tôi xác định hành vi cho
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
356 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
7>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
75Vì cả phép cộng và phép nhân đều có tính chất giao hoán, nên chúng ta có thể định nghĩa toán tử đảo ngược của chúng bằng cách gọi
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
50 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
355 trong >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
715 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
717 tương ứng. Mặt khác, hành vi của >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
716 cần được xác định vì phép trừ không có tính chất giao hoán>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
76Ghi chú. Bạn có thể nhận thấy rằng chúng tôi đã không thêm cấu trúc để xử lý phiên bản
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
725 tại đây. Điều này là do, trong trường hợp như vậy, cả hai toán hạng đều là thể hiện của lớp chúng ta và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
716 sẽ không chịu trách nhiệm xử lý thao tác. Thay vào đó, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
354 sẽ được gọi. Đây là một chi tiết tinh tế nhưng quan trọngBây giờ, chúng tôi xử lý hai toán tử,
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
754 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
755. Các phương pháp đặc biệt được sử dụng cho chúng lần lượt là >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
756 và >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
757. Hai số phức được gọi là bằng nhau nếu phần thực và phần ảo tương ứng của chúng bằng nhau. Chúng được cho là không bằng nhau khi một trong hai cái này không bằng nhau>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
77Ghi chú. Hướng dẫn dấu phẩy động là một bài viết nói về việc so sánh số float và độ chính xác của dấu phẩy động. Nó làm nổi bật những cảnh báo liên quan đến việc so sánh số float một cách trực tiếp, đó là điều chúng tôi đang làm ở đây
Cũng có thể nâng một số phức lên bất kỳ lũy thừa nào bằng công thức đơn giản. Chúng tôi định cấu hình hành vi cho cả toán tử
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
758 và toán tử >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
759 tích hợp bằng cách sử dụng phương pháp đặc biệt >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
760>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
78Ghi chú. Hãy xem kỹ định nghĩa của phương pháp. Chúng tôi đang gọi
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
00 để lấy mô đun của số phức. Vì vậy, một khi bạn đã xác định phương thức đặc biệt cho một hàm hoặc toán tử cụ thể trong lớp của mình, nó có thể được sử dụng trong các phương thức khác của cùng một lớpHãy tạo hai thể hiện của lớp này, một thể hiện có phần ảo dương và một thể hiện có phần ảo âm
>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
79biểu diễn chuỗi
>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
0Tái tạo đối tượng bằng cách sử dụng
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
313 với >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
310>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
1Phép cộng, phép trừ và phép nhân
>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
2Kiểm tra đẳng thức và bất đẳng thức
>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
3Cuối cùng, nâng một số phức lên một số lũy thừa
>>>
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
4Như bạn có thể thấy, các đối tượng của lớp tùy chỉnh của chúng ta hoạt động và trông giống như các đối tượng của lớp dựng sẵn và rất Pythonic. Mã ví dụ đầy đủ cho lớp này được nhúng bên dưới
Giải pháp. "Ví dụ hoàn chỉnh"Hiển thị/Ẩn
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
5Loại bỏ các quảng cáoTóm tắt và Tài nguyên
Trong hướng dẫn này, bạn đã tìm hiểu về Mô hình dữ liệu Python và cách Mô hình dữ liệu có thể được sử dụng để xây dựng các lớp Pythonic. Bạn đã tìm hiểu về cách thay đổi hành vi của các hàm tích hợp như
>>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
1, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
00, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order] # Calling len when no __len__
Traceback [most recent call last]:
File "", line 1, in
TypeError: object of type 'Order' has no len[]
07, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
329, v.v. Bạn cũng đã học về cách thay đổi hành vi của các toán tử tích hợp sẵn như >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
6, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
356, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
7, >>> class Order:
.. def __init__[self, cart, customer]:
.. self.cart = list[cart]
.. self.customer = customer
...
.. def __len__[self]:
.. return len[self.cart]
...
>>> order = Order[['banana', 'apple', 'mango'], 'Real Python']
>>> len[order]
3
759, v.v.Tiền thưởng miễn phí. Nhấp vào đây để có quyền truy cập vào Bảng cheat Python OOP miễn phí chỉ cho bạn các hướng dẫn, video và sách hay nhất để tìm hiểu thêm về Lập trình hướng đối tượng với Python
Sau khi đọc phần này, bạn có thể tự tin tạo các lớp sử dụng các tính năng thành ngữ tốt nhất của Python và biến các đối tượng của bạn thành Pythonic
Để biết thêm thông tin về Mô hình Dữ liệu cũng như quá tải hàm và toán tử, hãy xem các tài nguyên này
- Phần 3. 3, Tên phương thức đặc biệt của phần Mô hình dữ liệu trong tài liệu Python
- Thông thạo Python của Luciano Ramalho
- Thủ thuật Python. Quyển sách
Đánh dấu là đã hoàn thành
🐍 Thủ thuật Python 💌
Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python
Gửi cho tôi thủ thuật Python »
Về Malay Agarwal
Một người đam mê công nghệ với đầu óc triết học và một bàn tay có thể cầm bút
» Tìm hiểu thêm về tiếng Mã LaiMỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là
David
Đan
Geir Arne
Joanna
Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực
Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia
Nâng cao kỹ năng Python của bạn »
Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực
Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng các chuyên gia Pythonistas
Nâng cao kỹ năng Python của bạn »
Bạn nghĩ sao?
Đánh giá bài viết này
Tweet Chia sẻ Chia sẻ EmailBài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?
Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi