Module (69%)
Section (50%)

Python has come up with a module which provides the helper class for defining Abstract Base Classes (ABC) and that module name is abc.

The ABC allows you to mark classes as abstract ones and distinguish which methods of the base abstract class are abstract. A method becomes abstract by being decorated with an @abstractmethod decorator.

To start with ABC you should:

  1. import the abc module;
  2. make your base class inherit the helper class ABC, which is delivered by the abc module;
  3. decorate abstract methods with @abstractmethod, which is delivered by the abc module.
  4. When you run the code, the output doesn’t surprise anyone:

    Welcome to Green Field!

    output

    So far everything works fine, so let's play with the code a little.


Code

import abc

class BluePrint(abc.ABC):
@abc.abstractmethod
def hello(self):
pass

class GreenField(BluePrint):
def hello(self):
print('Welcome to Green Field!')


gf = GreenField()
gf.hello()
{{ dockerServerErrorMsg }} ×
{{ errorMsg }} ×
{{ successMsg }} ×