Introduction to Abstract Base Classes in Python

    From the previous article ( Introduction to duck typing and polymorphism in Python ), we learned about duck typing and polymorphism in python. Inheritance provides the basis for polymorphism, and polymorphism makes inheritance more flexible and powerful. So today, let's take a look at the abstract base class in python.

Abstract Base Classes (abc) in Python is a special class that defines a set of abstract methods that must be implemented in subclasses. The abstract base class itself cannot be instantiated, but is used to define the interface that the subclass needs to implement. In terms of definition, it is similar to the interface in java.

Before touching the abstract base class, let's take a look at how to control the subclass in python to override the method of the parent class after inheriting a certain class. If it is not rewritten, an exception will be thrown:

class Animail(object):
    def say(self):
        raise NotImplementedError




class Dog(Animail):
    def sayHi(self):
        print("我是一只猫,汪汪汪")




animal = Dog()
animal.say()  # raise NotImplementedError

As can be seen from the above script, if the say() method of the parent class is not rewritten in the Dog class, an exception prompt will only be thrown when the subclass object is instantiated and the say() method of the parent class is called.

Next, let's take a look at how to use the abstract base class in python to limit:

import abc


class Animail(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def say(self):
        pass


class Dog(Animail):
    def say(self):
        print("这里是小博测试成长之路的公众号,欢迎大家关注")


    def sayHi(self):
        print("我是一只猫,汪汪汪")


animal = Dog()
animal.say()

Of course, the above code can run normally, because the say() method of the base class Animail is rewritten in the Dog class.

From the above script, we need to grasp the following points:

1. In Python, a metaclass is a special class that is used to control the creation process of a class. When defining a class, the Python interpreter will automatically find the metaclass of the class, and then use the metaclass to create the class object. Metaclass can be regarded as the template of the class, which controls the creation process of the class, including the attributes, methods, and inheritance relationships of the class. Therefore, when class Animail(metaclass=abc.ABCMeta) defines a class like this, it indicates that the Animail class is an abstract base class, and the abstract base class itself cannot be instantiated directly .

8d7f50edf408cb643a74a95efce192b3.png

2. Use the annotation @abc.abstractmethod to indicate that the defined method is an abstract method, and the abstract method must be rewritten by subclasses. If it is not rewritten, an error will be reported when instantiating a subclass object:

2c6fe987d6da5599706f8758cb53e550.png

What is the difference between python's abstract base class and java's interface?

I have learned a little bit of java before, and found that the abstract base class I learned today is similar to the functions implemented by the interface in java. Now I have found a little difference:

1. Non-abstract methods can be defined in abstract base classes, but interfaces in java can only define method names and parameters, and cannot have specific implementations of methods.

python:

289619ae252df5fb84ee8a504fbd7f2b.png

java:

e57ac36526f4f721629e68f918a844ee.png

Of course, there may be some other differences. Due to my lack of talent and learning, I can’t understand it thoroughly for the time being. I can supplement and improve it later when I understand it thoroughly~

Click on the above text to add attention! Recommended index★★★★★

 Recently, I plan to set up a variety of check-in learning. Interested friends can chat privately on WeChat (xiaobotester) to study together.

Guess you like

Origin blog.csdn.net/liboshi123/article/details/129920395