Câu trả lời của Oddthinking là không sai, nhưng tôi nghĩ rằng nó bỏ lỡ lý do thực tế, thực tế Python có ABC trong một thế giới của việc gõ vịt.
Các phương pháp trừu tượng là gọn gàng, nhưng theo tôi, chúng không thực sự lấp đầy bất kỳ trường hợp sử dụng nào chưa được bao phủ bởi việc gõ vịt. Sức mạnh thực sự của các lớp cơ sở trừu tượng nằm ở cách chúng cho phép bạn tùy chỉnh hành vi của isinstance
và
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
0. .Mã nguồn của Python là mẫu mực. Dưới đây là cách
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
4 được xác định trong thư viện tiêu chuẩn [tại thời điểm viết]:class Container[metaclass=ABCMeta]:
__slots__ = []
@abstractmethod
def __contains__[self, x]:
return False
@classmethod
def __subclasshook__[cls, C]:
if cls is Container:
if any["__contains__" in B.__dict__ for B in C.__mro__]:
return True
return NotImplemented
Định nghĩa này của
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
1 nói rằng bất kỳ lớp nào có thuộc tính class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
6 đều được coi là một lớp con của container, ngay cả khi nó không phân lớp trực tiếp. Vì vậy, tôi có thể viết cái này:class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
Nói cách khác, nếu bạn thực hiện giao diện phù hợp, bạn là một lớp con! ABC cung cấp một cách chính thức để xác định các giao diện trong Python, trong khi vẫn đúng với tinh thần gõ vịt. Bên cạnh đó, điều này hoạt động theo cách tôn vinh nguyên tắc đóng cửa.
Mô hình đối tượng của Python trông rất giống với hệ thống OO "truyền thống" hơn [theo ý tôi là java*] - chúng tôi có các lớp yer, đối tượng yer, phương pháp yer - nhưng khi bạn gãi bề mặt, bạn sẽ tìm thấy thứ gì đó phong phú hơn và Linh hoạt hơn. Tương tự như vậy, khái niệm của Python về các lớp cơ sở trừu tượng có thể được nhận ra đối với một nhà phát triển Java, nhưng trong thực tế, chúng được dự định cho một mục đích rất khác.
Đôi khi tôi thấy mình viết các hàm đa hình có thể hoạt động trên một mục hoặc một bộ sưu tập các mục và tôi thấy
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
7 dễ đọc hơn nhiều so với class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
8 hoặc khối class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
9 tương đương. [Nếu bạn không biết Python, trong số ba người đó sẽ làm cho ý định của mã rõ ràng nhất?]Điều đó nói rằng, tôi thấy rằng tôi hiếm khi cần phải viết ABC của riêng mình và tôi thường khám phá ra sự cần thiết của một thông qua việc tái cấu trúc. Nếu tôi thấy một hàm đa hình tạo ra nhiều kiểm tra thuộc tính hoặc nhiều chức năng thực hiện kiểm tra thuộc tính tương tự, mùi đó cho thấy sự tồn tại của ABC đang chờ được trích xuất.
*Không tham gia vào cuộc tranh luận về việc Java có phải là hệ thống OO "truyền thống" ...
Phụ lục: Mặc dù một lớp cơ sở trừu tượng có thể ghi đè hành vi của isinstance
và
0, nhưng nó vẫn không vào MRO của lớp con ảo. Đây là một cạm bẫy tiềm năng cho khách hàng: không phải mọi đối tượng mà class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
2 đều có các phương thức được xác định trên class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
3.: Even though an abstract base class can override the behaviour of class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
isinstance
and
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
0, it still doesn't enter the
MRO of the virtual subclass. This is a potential pitfall for clients: not every object for which class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
2 has the methods defined on class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
3.class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
Thật không may, đây là một trong những cái bẫy "không làm điều đó" [trong đó Python có tương đối ít!]: Tránh xác định ABC với cả phương pháp
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
1 và không Abstract. Hơn nữa, bạn nên làm cho định nghĩa của bạn về class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
1 phù hợp với tập hợp các phương thức trừu tượng mà ABC của bạn xác định. Một lớp trừu tượng có thể được coi là một kế hoạch chi tiết cho các lớp khác. Nó cho phép bạn tạo một tập hợp các phương thức phải được tạo trong bất kỳ lớp con nào được xây dựng từ lớp trừu tượng. Một lớp chứa một hoặc nhiều phương thức trừu tượng được gọi là một lớp trừu tượng. Một phương pháp trừu tượng là một phương pháp có khai báo nhưng không có việc thực hiện. Trong khi chúng tôi đang thiết kế các đơn vị chức năng lớn, chúng tôi sử dụng một lớp trừu tượng. Khi chúng tôi muốn cung cấp một giao diện chung cho các triển khai khác nhau của một thành phần, chúng tôi sử dụng một lớp trừu tượng. [API] cho một tập hợp các lớp con. Khả năng này đặc biệt hữu ích trong các tình huống mà bên thứ ba sẽ cung cấp các triển khai, chẳng hạn như với các plugin, nhưng cũng có thể giúp bạn khi làm việc trong một nhóm lớn hoặc với một cơ sở mã lớn trong đó giữ cho tất cả các lớp trong tâm trí của bạn là khó khăn hoặc không thể. Python đi kèm với một mô -đun cung cấp cơ sở để xác định các lớp cơ sở trừu tượng [ABC] và tên mô -đun đó là ABC. ABC hoạt động bằng cách trang trí các phương pháp của lớp cơ sở là trừu tượng và sau đó đăng ký các lớp bê tông dưới dạng triển khai cơ sở trừu tượng. Một phương pháp trở nên trừu tượng khi được trang trí bằng từ khóa @abstractmethod. Ví dụ - & nbsp;
Why use Abstract Base Classes :
By defining an abstract base class, you can define a common Application Program Interface[API] for a set of subclasses. This capability is especially useful in situations where a third-party is going to provide implementations, such as with plugins, but can also help you when working in a large team or with a large code-base where keeping all classes in
your mind is difficult or not possible.
How Abstract Base classes work :
By default, Python does not provide abstract classes. Python comes with a module that provides the base for defining Abstract Base classes[ABC] and that module name is ABC. ABC works by decorating methods of the base class as abstract and then registering concrete classes as implementations of the abstract base. A method becomes abstract when
decorated with the keyword @abstractmethod. For Example –
Mã 1:
Python3
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
6 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
7class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
8 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
9I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I have 3 sides I have 4 sides I have 5 sides I have 6 sides1
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides3
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar0
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I can walk and run I can crawl I can bark I can roar2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
True True1
True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I can walk and run I can crawl I can bark I can roar2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
True True4
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
Abstract Base Class subclass3
True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
Can't instantiate abstract class parent with abstract methods geeks child class5
True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I can walk and run I can crawl I can bark I can roar2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
Traceback [most recent call last]: File "/home/ffe4267d930f204512b7f501bb1bc489.py", line 19, in c=Animal[] TypeError: Can't instantiate abstract class Animal with abstract methods move7
True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I can walk and run I can crawl I can bark I can roar2
isinstance
2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
True True4
isinstance
6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
isinstance
2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
Abstract Base Class subclass6
isinstance
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0 Can't instantiate abstract class parent with abstract methods geeks
child class
8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides
Traceback [most recent call last]: File "/home/ffe4267d930f204512b7f501bb1bc489.py", line 19, in c=Animal[] TypeError: Can't instantiate abstract class Animal with abstract methods move9
isinstance
0 isinstance
1Code 2:
Python3
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
6 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
7class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
8 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
9I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I have 3 sides I have 4 sides I have 5 sides I have 6 sides1
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar0
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I can walk and run I can crawl I can bark I can roar2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
28True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I can walk and run I can crawl I can bark I can roar2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
40True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I can walk and run I can crawl I can bark I can roar2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
52True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I can walk and run I can crawl I can bark I can roar2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
64True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
I can walk and run I can crawl I can bark I can roar2
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
69I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
True True4
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
73I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
I have 3 sides I have 4 sides I have 5 sides I have 6 sides6
I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
69I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
Abstract Base Class subclass6
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
73Output:
I can walk and run I can crawl I can bark I can roar
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
Can't instantiate abstract class parent with abstract methods geeks child class8
Implementation Through Subclassing :
By subclassing directly from the base, we can avoid the need to register the class explicitly. In this case, the Python class management is used to recognize PluginImplementation as implementing the abstract PluginBase.
Python3
Traceback [most recent call last]: File "/home/ffe4267d930f204512b7f501bb1bc489.py", line 19, in c=Animal[] TypeError: Can't instantiate abstract class Animal with abstract methods move9
isinstance
0 isinstance
1isinstance
3isinstance
0 isinstance
5
Traceback [most recent call last]: File "/home/ffe4267d930f204512b7f501bb1bc489.py", line 19, in c=Animal[] TypeError: Can't instantiate abstract class Animal with abstract methods move9
isinstance
0 isinstance
9I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar0
isinstance
3isinstance
0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
03Traceback [most recent call last]: File "/home/ffe4267d930f204512b7f501bb1bc489.py", line 19, in c=Animal[] TypeError: Can't instantiate abstract class Animal with abstract methods move9
isinstance
0 isinstance
9I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
03True True2
I can walk and run I can crawl I can bark I can roar9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
06class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
0class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
08I can walk and run I can crawl I can bark I can roar9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
06isinstance
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
12Output:
True True
isinstance
3isinstance
0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
03Concrete Methods in Abstract Base Classes :
Concrete classes contain only concrete [normal]methods whereas abstract classes may contain both concrete methods and abstract methods. The concrete class provides an implementation of abstract methods, the abstract base class can also provide an implementation by invoking the methods via super[].
Hãy xem qua ví dụ để gọi phương thức bằng Super []: & nbsp; & nbsp;
Python3
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
8 class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
83class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
6 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
7class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
8 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
9I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
20I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
23I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
29True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
32I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
23I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
39class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
40I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
44True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
32class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
49Output:
Abstract Base Class subclass
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
46isinstance
0 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
48Abstract Properties :
Abstract classes include attributes in addition to methods, you can require the attributes in concrete classes by defining them with @abstractproperty.
Python3
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
8 class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
83class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
6 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
7class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
8 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
9I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
20I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
59class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
60I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
23I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
32class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
46isinstance
0 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
48I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
72I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
23I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
32class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
81class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
82I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
46isinstance
0class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
86I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I can walk and run I can crawl I can bark I can roar9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
89class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
46isinstance
0 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
48Trong chương trình trên, chúng ta có thể gọi các phương thức trong các lớp trừu tượng bằng cách sử dụng Super []. & NBSP; & nbsp; & nbsp; thuộc tính trừu tượng: & nbsp; các lớp trừu tượng bao gồm các thuộc tính ngoài các phương thức, bạn có thể yêu cầu các thuộc tính trong các lớp cụ thể bằng cách xác định chúng với @Tóm tắtProperty. & NBSP; & NBSP;
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
57I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides5
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
88I have 3 sides I have 4 sides I have 5 sides I have 6 sides7
I have 3 sides I have 4 sides I have 5 sides I have 6 sides8
Output:
Can't instantiate abstract class parent with abstract methods geeks child class
I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
67 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
68Abstract Class Instantiation :
Abstract classes are incomplete because they have methods that have nobody. If python allows creating an object for abstract classes then using that object if anyone calls the abstract method, but there is no actual implementation to invoke. So we use an abstract class as a template and according to the need, we extend it and build on it before we can use it. Due to the fact, an abstract class is not a concrete class, it cannot be instantiated. When we create an object for the abstract class it raises an error.
Python3
I have 3 sides I have 4 sides I have 5 sides I have 6 sides0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
94I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
67 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
03I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides3
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
90 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
91I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar0
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I can walk and run I can crawl I can bark I can roar9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
94class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
90 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
91I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
28True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I can walk and run I can crawl I can bark I can roar9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
94class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
90 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
91I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
40True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I can walk and run I can crawl I can bark I can roar9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
94class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
90 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
91I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
52True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides2
I can walk and run I can crawl I can bark I can roar9
class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
94class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
90 class MyABC[metaclass=abc.ABCMeta]:
def abc_method[self]:
pass
@classmethod
def __subclasshook__[cls, C]:
return True
class C[object]:
pass
# typical client code
c = C[]
if isinstance[c, MyABC]: # will be true
c.abc_method[] # raises AttributeError
91I have 3 sides I have 4 sides I have 5 sides I have 6 sides9
I can walk and run I can crawl I can bark I can roar9
True True0
class ContainAllTheThings[object]:
def __contains__[self, item]:
return True
>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
64True True2
I have 3 sides I have 4 sides I have 5 sides I have 6 sides63
isinstance
0I have 3 sides I have 4 sides I have 5 sides I have 6 sides65
Output:
Traceback [most recent call last]: File "/home/ffe4267d930f204512b7f501bb1bc489.py", line 19, in c=Animal[] TypeError: Can't instantiate abstract class Animal with abstract methods move