- Hands-On Penetration Testing with Python
- Furqan Khan
- 413字
- 2021-07-02 14:13:59
Abstract classes
There are many occasions in which we may want to have partial implementation of a class such that the class defines its objective with a template and it also defines how it must obtain a portion of its objective with the help of a few implemented methods. The remaining portion of the class objective can be left out to be implemented by the subclass, which is mandatory. To implement use cases such as this, we make use of abstract classes. An abstract base class, popularly known as an abc class, is a class that contains abstract methods. An abstract method is a method that does not have an implementation. It simply contains the deceleration and is meant to be implemented in the class that would implement or inherit from the abstract class.
A few important pointers about abstract classes include the following:
- An abstract method in Python is declared with the @abstractmethod decorator.
- While an abstract class can contain abstract methods, nothing prevents an abstract class from having normal or non-abstract methods as well.
- An abstract class cannot be instantiated.
- The subclass of the abstract class must implement all the abstract methods of the base class. Failing this, it can't be instantiated.
- If the subclass of an abstract class doesn't implement the abstract methods, it automatically becomes an abstract class, which can be then further extended by another class.
- Abstract classes in Python are implemented using the abc module.
Let's create a class called Abstract.py and take a look at how abstract classes can be used in Python:

In the preceding example, we created an abstract class called QueueAbs that inherits from the Abstract base class, called ABC. The class has got two abstract methods, enqueue and dequeue, and also one concrete method called printItems(). Then, we created a class called Queue that is a subclass of the QueueAbs abstract base class and that implements the enqueue and dequeue methods. Finally, we make the instance of the Queue class and invoke the methods, as shown previously.
- Expert C++
- 精通Nginx(第2版)
- Learning Docker
- Python 深度學(xué)習(xí)
- Scratch真好玩:教小孩學(xué)編程
- iOS開發(fā)實(shí)戰(zhàn):從入門到上架App Store(第2版) (移動(dòng)開發(fā)叢書)
- Python爬蟲、數(shù)據(jù)分析與可視化:工具詳解與案例實(shí)戰(zhàn)
- Spring+Spring MVC+MyBatis從零開始學(xué)
- Django 2.0 入門與實(shí)踐
- 程序員超強(qiáng)大腦
- Hands-On Game Development Patterns with Unity 2019
- 交互設(shè)計(jì)語(yǔ)言:與萬(wàn)物對(duì)話的藝術(shù)(全兩冊(cè))
- Modular Programming with PHP 7
- 數(shù)據(jù)結(jié)構(gòu):C語(yǔ)言描述(融媒體版)
- 湖南省大學(xué)生計(jì)算機(jī)程序設(shè)計(jì)競(jìng)賽試題與應(yīng)用軟件開發(fā)類作品集萃(2005—2016)