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] True0 hoặc
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True1 để 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] True4 không cần định nghĩa
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True7 vì toán tử , iteration logic, and the
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True8 function automatically fall back to using
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True9 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 inherit0.
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 inherit1]
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True4
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5
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] True9,
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 inherit0 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6 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 inherit5 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 inherit6
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 inherit7. 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 inherit8 1
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True0 1
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True1
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True2 1 2
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True4 1
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True2
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True6
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True8 1
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True2
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True7
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True41 1
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True4
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True43,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True44
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True45,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6,
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True6
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True48 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 inherit0
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True50 1
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True51
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True52 1
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True48,
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True2,
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 inherit8
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6,
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 inherit0
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 inherit5
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True8,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True52
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True9,
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 inherit0
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True7,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True17 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True18
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True19
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 inherit5
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True9,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True62,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True63,
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 inherit0,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True65
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 inherit5 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True67,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True68,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True69,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True240,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True241 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True242
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True243
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 inherit5
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True9,
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 inherit0
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 inherit5 được kế thừa
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True248
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True52
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6,
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 inherit0
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True293,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True294,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True295,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True296,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True297,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True298,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True299,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True00,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True01,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True02 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True03
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True04
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True248
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6,
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 inherit0,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True09,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True10
Kế thừa các phương pháp
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True248 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True12,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True240,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True241,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True15,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True16,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True17 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True18
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 inherit6
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True52
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True9,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6,
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 inherit0
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True25,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True26,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True27,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True28,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True295 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True296
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True31
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 inherit6
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True9,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True62,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True63,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6,
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 inherit0
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 inherit6 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True240,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True40,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True12,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True42 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True43
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True44
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True48
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 inherit0
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True47
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True44,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True248
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True52
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True44,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True248
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True57
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True44,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True52
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True5,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True62 1
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True63
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True64 1
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True62
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True43,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True44
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True45
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True69 1
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True70
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True71 1
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True69
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True73
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True70
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True75 1
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True71
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True77,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True78
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True79,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True70,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True73
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] True82 để 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 inherit1. Đ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ếp2
Kiểm tra
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True84 phát hiện các lớp được đăng ký là
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True2 hoặc có phương thức
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True86, 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] True87. 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] True88.
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] True89lớ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] True90lớ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] True91lớ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] True92lớ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] True86
Kiểm tra
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True84 phát hiện các lớp được đăng ký là
>>> issubclass[D, Sequence] True >>> isinstance[D[], Sequence] True2 hoặc có phương thức
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True86, 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] True87. 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] True88. 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] True86 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 inherit00. 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 inherit01
Mới trong phiên bản 3. 6
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 inherit02,
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 inherit03 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 inherit04. 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ỗi ¶ lớp bộ sưu tập. abc. MutableSequence ¶ lớ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] True86,
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 inherit01 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 inherit07, 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] True87 bên dưới. Do đó, nếu
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True87 đượ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. ItemsView ¶ lớp bộ sưu tập. abc. KeysView ¶ lớ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 inherit10. 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 inherit11.
Coroutine của
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True64 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 inherit13] 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 inherit11. 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 inherit15 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 inherit16. 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 inherit17 để 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 inherit02,
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 inherit03 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 inherit04. 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 inherit11. Tất cả các phiên bản
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True64 cũng là phiên bản của
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True62. 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 inherit13] 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 inherit11. 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 inherit26 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 inherit16. 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 inherit17 để 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] True70. 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] True70 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True73. 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] True1
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] True248 đầ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] True89,
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True86 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True91. 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 inherit36 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 inherit37
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True6
Lưu ý khi sử dụng
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True248 và
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True04 làm mixin
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ọiclass 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ênclass 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Để 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 theoMixin
>>> issubclass[C, Sequence] True >>> isinstance[C[], Sequence] True
248 cung cấp phương thứcclass 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 địnhclass 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
52Nhữ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.