Python - Abstract Base Classes (ABCs)

> Procedural Languages > Python > Python - Data Type

1 - About

3 - Management

3.1 - Creation

Use the ABCMeta metaclass to create an ABC.

from abc import ABCMeta, abstractmethod
 
class MyAbstractClass(object):
 
        # specifying that is an abstract class
	__metaclass__ = ABCMeta
 
        # An attribute
        name = None
 
        def __init__(self, name):
             self.name = name
 
	@abstractmethod
	def my_abstract_method(self):
		"""
		blablabla
		:return: None
		"""
		return
Advertising

3.2 - Implementation

class MyClassImplementation(MyAbstractClass):
 
    def __init__(self):
        super().__init__("name")
 
    def my_abstract_method(self):
        """
        echo name
        :return: None
        """
        print (self.name)
        return