Hướng dẫn benefits of abstract class in python - lợi ích của lớp trừu tượng trong python

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

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

class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
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 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 đề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 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
7
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 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
9

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
1

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
3

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
0

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I can walk and run
I can crawl
I can bark
I can roar
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
True
True
1
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I can walk and run
I can crawl
I can bark
I can roar
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
True
True
4

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
Abstract Base Class
subclass
3
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
Can't instantiate abstract class parent with abstract methods geeks
child class
5
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I can walk and run
I can crawl
I can bark
I can roar
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
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
7
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I can walk and run
I can crawl
I can bark
I can roar
2

isinstance2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
True
True
4

isinstance6

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

isinstance2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
Abstract Base Class
subclass
6

isinstance6

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 move
9isinstance0 isinstance1
Code 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
7
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 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
9

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
1

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
0

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I can walk and run
I can crawl
I can bark
I can roar
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
28
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I can walk and run
I can crawl
I can bark
I can roar
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
40
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I can walk and run
I can crawl
I can bark
I can roar
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
52
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I can walk and run
I can crawl
I can bark
I can roar
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
64
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
I can walk and run
I can crawl
I can bark
I can roar
2

class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
69

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
True
True
4

class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
73

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
6
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
69

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
0
Abstract Base Class
subclass
6

class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
73

Output:   
 

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 sides
0
Can't instantiate abstract class parent with abstract methods geeks
child class
8
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 move
9isinstance0 isinstance1

isinstance3isinstance0 isinstance5

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
9isinstance0 isinstance9

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
0

isinstance3isinstance0

class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
03

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
9isinstance0 isinstance9

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
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
03
True
True
2

I can walk and run
I can crawl
I can bark
I can roar
9
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
06
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
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
08

I can walk and run
I can crawl
I can bark
I can roar
9
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
12

Output:   
 

True
True

isinstance3isinstance0

class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
03
  
Concrete 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
83

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
7
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 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
9

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
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
20

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
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
23
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
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
29
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
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
32

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
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
23
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
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
39
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
40

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
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
44
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
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
32

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
49

Output:   
 

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
46isinstance0
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
48
  
Abstract 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
83

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
7
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 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
9

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
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
20

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
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
59
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
60

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
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
23
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
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
32

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
46isinstance0
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
48

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
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
72

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
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
23
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
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
32

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
81
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
82

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
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
46isinstance0
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
86

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I can walk and run
I can crawl
I can bark
I can roar
9
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
89

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
46isinstance0
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
48

Trong 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 sides
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
57

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
5
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
88
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
7
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
8

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 sides
9
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
68
  
Abstract 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 sides
0
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
94

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
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
03

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
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
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
91

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
0

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I can walk and run
I can crawl
I can bark
I can roar
9
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
94

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
91

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
28
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I can walk and run
I can crawl
I can bark
I can roar
9
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
94

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
91

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
40
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I can walk and run
I can crawl
I can bark
I can roar
9
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
94

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
91

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
52
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
2
I can walk and run
I can crawl
I can bark
I can roar
9
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
94

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
91

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
9
I can walk and run
I can crawl
I can bark
I can roar
9
True
True
0
class ContainAllTheThings[object]:
    def __contains__[self, item]:
        return True

>>> issubclass[ContainAllTheThings, collections.Container]
True
>>> isinstance[ContainAllTheThings[], collections.Container]
True
64
True
True
2

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
63isinstance0
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
65

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

Những lợi ích của lớp trừu tượng là gì?

Ưu điểm của các lớp trừu tượng..
Lớp trừu tượng trong Java rất có lợi khi viết mã ngắn hơn ..
Trừu tượng trong java tránh sao chép mã ..
Các lớp trừu tượng cho phép tái sử dụng mã ..
Các thay đổi đối với việc thực hiện mã nội bộ được thực hiện mà không ảnh hưởng đến các lớp ..

Mục đích của các lớp trừu tượng là gì?

Một lớp trừu tượng không thể được khởi tạo. Mục đích của một lớp trừu tượng là cung cấp một định nghĩa chung về một lớp cơ sở mà nhiều lớp dẫn xuất có thể chia sẻ.to provide a common definition of a base class that multiple derived classes can share.

Khi nào tôi nên sử dụng Python lớp trừu tượng?

Thông thường, bạn sử dụng một lớp trừu tượng để tạo bản thiết kế cho các lớp khác.Tương tự, một phương pháp trừu tượng là một phương thức không có triển khai.Một lớp trừu tượng có thể hoặc không bao gồm các phương pháp trừu tượng.Python không trực tiếp hỗ trợ các lớp trừu tượng.to create a blueprint for other classes. Similarly, an abstract method is an method without an implementation. An abstract class may or may not include abstract methods. Python doesn't directly support abstract classes.

Tại sao sự trừu tượng lại quan trọng trong Python?

Tại sao sự trừu tượng là quan trọng?Trong Python, một sự trừu tượng được sử dụng để ẩn dữ liệu/lớp không liên quan để giảm độ phức tạp.Nó cũng tăng cường hiệu quả ứng dụng.Tiếp theo, chúng ta sẽ tìm hiểu làm thế nào chúng ta có thể đạt được sự trừu tượng bằng chương trình Python.to hide the irrelevant data/class in order to reduce the complexity. It also enhances the application efficiency. Next, we will learn how we can achieve abstraction using the Python program.

Bài Viết Liên Quan

Chủ Đề