Python Abc Example - Python

What is abstract base class in python?

Snippet Code


  
Rate this page :
  [ 0 votes]

Abc is abbreviated as abstract base class in python. Abstract class can not instantiate with abstract methods. Abc is a form of interface checking which checks for particular methods. It works by using methods of base class as abstract. Here, ABCWithConcreteImplementation is an abstract base class, which is not possible to instantiate or to use it directly.

abc.py: import abc from cStringIO import StringIO class ABCWithConcreteImplementation(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def retrieve_values(self, input): print 'base class reading data' return input.read() class ConcreteOverride(ABCWithConcreteImplementation): def retrieve_values(self, input): base_data = super(ConcreteOverride, self).retrieve_values(input) print 'subclass sorting data' response = sorted(base_data.splitlines()) return response input = StringIO("""line one line two line three """) reader = ConcreteOverride() print reader.retrieve_values(input) print To run the code: $ python abc.py

Tags


Ask Questions

Ask Question