Các lớp Python nào có thể lặp lại?

1] Một lớp mới được viết có thể kế thừa trực tiếp từ một trong các lớp cơ sở trừu tượng. Lớp phải cung cấp các phương thức trừu tượng cần thiết. Các phương thức mixin còn lại đến từ sự kế thừa và có thể được ghi đè nếu muốn. Các phương pháp khác có thể được thêm vào khi cần thiết

class C[Sequence]:                      # Direct inheritance
    def __init__[self]: ...             # Extra method not required by the ABC
    def __getitem__[self, index]:  ...  # Required abstract method
    def __len__[self]:  ...             # Required abstract method
    def count[self, value]: ...         # Optionally override a mixin method

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True

2] Các lớp hiện có và các lớp dựng sẵn có thể được đăng ký dưới dạng “các lớp con ảo” của ABC. Các lớp đó phải xác định API đầy đủ bao gồm tất cả các phương thức trừu tượng và tất cả các phương thức mixin. Điều này cho phép người dùng dựa vào các bài kiểm tra

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
0 hoặc
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
1 để xác định xem toàn bộ giao diện có được hỗ trợ hay không. Ngoại lệ đối với quy tắc này dành cho các phương thức được tự động suy ra từ phần còn lại của API

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True

Trong ví dụ này, lớp

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
4 không cần định nghĩa
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
7 vì toán tử , iteration logic, and the
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
8 function automatically fall back to using
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
9 and
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0.

3] Một số giao diện đơn giản có thể nhận dạng trực tiếp bằng sự hiện diện của các phương thức cần thiết [trừ khi các phương thức đó đã được đặt thành

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
1]

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
4

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5

Các giao diện phức tạp không hỗ trợ kỹ thuật cuối cùng này vì một giao diện không chỉ có sự hiện diện của các tên phương thức. Các giao diện chỉ định ngữ nghĩa và mối quan hệ giữa các phương thức không thể suy ra chỉ từ sự hiện diện của các tên phương thức cụ thể. Ví dụ: biết rằng một lớp học cung cấp

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
9,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6 là không đủ để phân biệt một
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
5 với một
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
6

Mới trong phiên bản 3. 9. Các lớp trừu tượng này hiện hỗ trợ

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
7. Xem Loại bí danh chung và PEP 585.

Bộ sưu tập Các lớp cơ sở trừu tượng¶

Mô-đun bộ sưu tập cung cấp ABC sau.

ABC

Kế thừa từ

phương pháp trừu tượng

Phương pháp Mixin

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
8 1

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
0 1

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
1

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
2 1 2

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
4 1

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
2

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
6

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
8 1

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
2

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
7

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
41 1

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
4

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
43,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
44

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
45,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6,
>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
6

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
48 1

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
50 1

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
51

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
52 1

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
48,
>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
2,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
8

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
5

>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
8,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
52

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
9,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
7,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
17 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
18

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
19

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
5

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
9,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
62,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
63,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
65

Các phương thức kế thừa của

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
5 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
67,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
68,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
69,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
240,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
241 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
242

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
243

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
5

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
9,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0

Các phương thức

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
5 được kế thừa

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
248

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
52

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
293,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
294,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
295,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
296,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
297,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
298,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
299,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
00,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
01,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
02 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
03

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
04

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
248

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
09,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
10

Kế thừa các phương pháp

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
248 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
12,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
240,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
241,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
15,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
16,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
17 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
18

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
6

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
52

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
9,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
25,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
26,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
27,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
28,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
295 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
296

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
31

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
6

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
9,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
62,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
63,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0

Kế thừa các phương thức

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
6 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
240,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
40,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
12,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
42 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
43

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
44

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
48

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
0

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
47

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
44,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
248

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
52

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
44,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
248

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
57

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
44,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
52

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
5,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
62 1

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
63

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
64 1

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
62

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
43,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
44

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
45

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
69 1

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
70

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
71 1

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
69

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
73

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
70

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
75 1

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
71

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
77,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
78

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
79,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
70,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
73

chú thích

1[1,2,3,4,5,6,7,8,9,10,11,12,13,14]

Các ABC này ghi đè

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
82 để hỗ trợ kiểm tra giao diện bằng cách xác minh các phương thức cần thiết có mặt và chưa được đặt thành
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
1. Điều này chỉ hoạt động cho các giao diện đơn giản. Các giao diện phức tạp hơn yêu cầu đăng ký hoặc phân lớp trực tiếp

2

Kiểm tra

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
84 phát hiện các lớp được đăng ký là
>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
2 hoặc có phương thức
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
86, nhưng không phát hiện các lớp lặp lại với phương thức
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
87. Cách đáng tin cậy duy nhất để xác định xem một đối tượng có có thể lặp lại hay không là gọi
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
88.

Bộ sưu tập Các lớp cơ sở trừu tượng – Mô tả chi tiết¶

lớp bộ sưu tập. abc. Vùng chứa

ABC cho các lớp cung cấp phương thức

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
89

lớp bộ sưu tập. abc. Có thể băm

ABC cho các lớp cung cấp phương thức

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
90

lớp bộ sưu tập. abc. Kích cỡ

ABC cho các lớp cung cấp phương thức

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
91

lớp bộ sưu tập. abc. Có thể gọi được

ABC cho các lớp cung cấp phương thức

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
92

lớp bộ sưu tập. abc. Có thể lặp lại

ABC cho các lớp cung cấp phương thức

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
86

Kiểm tra

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
84 phát hiện các lớp được đăng ký là
>>> issubclass[D, Sequence]
True
>>> isinstance[D[], Sequence]
True
2 hoặc có phương thức
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
86, nhưng không phát hiện các lớp lặp lại với phương thức
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
87. Cách đáng tin cậy duy nhất để xác định xem một đối tượng có có thể lặp lại hay không là gọi
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
88.

lớp bộ sưu tập. abc. Bộ sưu tập

ABC cho các lớp vùng chứa có thể lặp lại có kích thước

Mới trong phiên bản 3. 6

lớp bộ sưu tập. abc. Trình lặp

ABC cho các lớp cung cấp phương thức

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
86 và
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
00. Xem thêm định nghĩa của iterator .

lớp bộ sưu tập. abc. Có thể đảo ngược

ABC cho các lớp có thể lặp lại cũng cung cấp phương thức

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
01

Mới trong phiên bản 3. 6

lớp bộ sưu tập. abc. Máy phát điện

ABC cho các lớp trình tạo triển khai giao thức được xác định trong PEP 342 mở rộng các trình vòng lặp bằng các phương thức

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
02,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
03 và
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
04. Xem thêm định nghĩa của trình tạo .

Mới trong phiên bản 3. 5

lớp bộ sưu tập. abc. Chuỗilớp bộ sưu tập. abc. MutableSequencelớp bộ sưu tập. abc. ByteString

ABC cho trình tự chỉ đọc và có thể thay đổi .

lưu ý thực hiện. Một số phương thức mixin, chẳng hạn như

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
86,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
01 và
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
07, thực hiện các cuộc gọi lặp lại tới phương thức
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
87 bên dưới. Do đó, nếu
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
87 được triển khai với tốc độ truy cập không đổi, các phương thức mixin sẽ có hiệu suất tuyến tính;

Đã thay đổi trong phiên bản 3. 5. Phương thức index[] đã thêm hỗ trợ cho các đối số dừng và bắt đầu.

lớp bộ sưu tập. abc. Bộlớp bộ sưu tập. abc. MutableSet

ABC cho các bộ chỉ đọc và có thể thay đổi

lớp bộ sưu tập. abc. Ánh xạlớp bộ sưu tập. abc. Ánh xạ có thể thay đổi

ABC cho ánh xạ chỉ đọc và có thể thay đổi .

lớp bộ sưu tập. abc. Chế độ xem ánh xạlớp bộ sưu tập. abc. ItemsViewlớp bộ sưu tập. abc. KeysViewlớp bộ sưu tập. abc. Chế độ xem giá trị

ABC để ánh xạ, mục, khóa và giá trị chế độ xem .

lớp bộ sưu tập. abc. Đang chờ đợi

ABC cho các đối tượng awaitable , có thể được sử dụng trong biểu thức

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
10. Việc triển khai tùy chỉnh phải cung cấp phương thức
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
11.

Coroutine của

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
64 ABC đều là phiên bản của ABC này.

Ghi chú

Trong CPython, các coroutine dựa trên trình tạo [trình tạo được trang trí bằng

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
13] có thể chờ được, mặc dù chúng không có phương thức
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
11. Sử dụng
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
15 cho chúng sẽ trả về
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
16. Sử dụng
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
17 để phát hiện chúng

Mới trong phiên bản 3. 5

lớp bộ sưu tập. abc. Coroutine

ABC cho các lớp tương thích với coroutine. Chúng triển khai các phương thức sau, được xác định trong Đối tượng Coroutine .

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
02,
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
03 và
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
04. Triển khai tùy chỉnh cũng phải triển khai
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
11. Tất cả các phiên bản
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
64 cũng là phiên bản của
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
62. Xem thêm định nghĩa của coroutine .

Ghi chú

Trong CPython, các coroutine dựa trên trình tạo [trình tạo được trang trí bằng

class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
13] có thể chờ được, mặc dù chúng không có phương thức
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
11. Sử dụng
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
26 cho chúng sẽ trả về
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
16. Sử dụng
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
17 để phát hiện chúng

Mới trong phiên bản 3. 5

lớp bộ sưu tập. abc. AsyncIterable

ABC cho các lớp cung cấp phương thức

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
70. Xem thêm định nghĩa về có thể lặp lại không đồng bộ .

Mới trong phiên bản 3. 5

lớp bộ sưu tập. abc. AsyncIterator

ABC cho các lớp cung cấp các phương thức

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
70 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
73. Xem thêm định nghĩa của trình lặp không đồng bộ .

Mới trong phiên bản 3. 5

lớp bộ sưu tập. abc. AsyncGenerator

ABC cho các lớp trình tạo không đồng bộ triển khai giao thức được xác định trong PEP 525 và PEP 492

Mới trong phiên bản 3. 6

Ví dụ và Công thức¶

ABC cho phép chúng tôi hỏi các lớp hoặc trường hợp nếu chúng cung cấp chức năng cụ thể, chẳng hạn

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
1

Một số ABC cũng hữu ích như mixin giúp phát triển các lớp hỗ trợ API vùng chứa dễ dàng hơn. Ví dụ: để viết một lớp hỗ trợ API

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
248 đầy đủ, chỉ cần cung cấp ba phương thức trừu tượng cơ bản.
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
89,
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
86 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
91. ABC cung cấp các phương thức còn lại như
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
36 và
class D:                                 # No inheritance
    def __init__[self]: ...              # Extra method not required by the ABC
    def __getitem__[self, index]:  ...   # Abstract method
    def __len__[self]:  ...              # Abstract method
    def count[self, value]: ...          # Mixin method
    def index[self, value]: ...          # Mixin method

Sequence.register[D]                     # Register instead of inherit
37

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
6

Lưu ý khi sử dụng

>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
248 và
>>> issubclass[C, Sequence]
True
>>> isinstance[C[], Sequence]
True
04 làm mixin

  1. Vì một số hoạt động thiết lập tạo ra các tập hợp mới, các phương thức mixin mặc định cần một cách để tạo các phiên bản mới từ một lần lặp. Hàm tạo của lớp được giả sử có chữ ký ở dạng

    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    40. Giả định đó được đưa ra đối với một phương pháp phân loại nội bộ có tên là
    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    41, phương pháp này gọi
    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    42 để tạo ra một tập hợp mới. Nếu mixin
    >>> issubclass[C, Sequence]
    True
    >>> isinstance[C[], Sequence]
    True
    
    248 đang được sử dụng trong một lớp có chữ ký hàm tạo khác, bạn sẽ cần ghi đè lên
    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    41 bằng một phương thức lớp hoặc phương thức thông thường có thể tạo các thể hiện mới từ một đối số có thể lặp lại

  2. Để ghi đè các phép so sánh [có lẽ là về tốc độ, vì ngữ nghĩa đã được sửa], hãy xác định lại

    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    45 và
    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    46, sau đó các hoạt động khác sẽ tự động tuân theo

  3. Mixin

    >>> issubclass[C, Sequence]
    True
    >>> isinstance[C[], Sequence]
    True
    
    248 cung cấp phương thức
    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    48 để tính giá trị băm cho tập hợp; . Để thêm khả năng băm đã đặt bằng cách sử dụng mixin, hãy kế thừa từ cả
    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    50 và
    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    51, sau đó xác định
    class D:                                 # No inheritance
        def __init__[self]: ...              # Extra method not required by the ABC
        def __getitem__[self, index]:  ...   # Abstract method
        def __len__[self]:  ...              # Abstract method
        def count[self, value]: ...          # Mixin method
        def index[self, value]: ...          # Mixin method
    
    Sequence.register[D]                     # Register instead of inherit
    
    52

    Những đối tượng Python nào có thể lặp lại?

    Danh sách, bộ dữ liệu, từ điển và bộ đều là các đối tượng có thể lặp lại. Chúng là các thùng chứa có thể lặp lại mà bạn có thể lấy một trình vòng lặp từ.

    Lớp lặp lại là gì?

    Tập hợp các giá trị hoặc "phần tử" có thể được truy cập tuần tự . Các phần tử của iterable được truy cập bằng cách lấy một Iterator bằng cách sử dụng iterator getter và sử dụng nó để chuyển qua các giá trị.

    Bạn có thể lặp qua một lớp Python không?

    Có thể đạt được phép lặp trong các lớp được xác định tùy chỉnh của bạn bằng cách bao gồm cả phương thức lặp và phương thức tiếp theo hoặc chỉ cần trả về trình tạo trong phương thức lặp .

    Đối tượng Python nào không thể lặp lại?

    Lỗi loại Python. NoneType Object Is Not Iterable là một ngoại lệ xảy ra khi cố gắng lặp lại một giá trị None. Vì trong Python, chỉ các đối tượng có giá trị mới có thể được lặp lại, việc lặp lại trên một đối tượng Không có sẽ làm tăng TypeError. Không có loại đối tượng là ngoại lệ không thể lặp lại.

Chủ Đề