Scott R.
asked 05/16/18
Does Python supports interfaces like in Java? Discuss.
Virgilio D.
answered 05/16/18
Senior Software Engineer Specializing in Python.
An interface has methods that have no implementations. In Python, Abstract Base Classes provide this functionality. One has to do the following:
- import abc
- create a class to be used as the interface, with the following characteristics:
- define the __metaclass__ = abc.ABCMeta
- decorate the empty methods as @abc.abstractmethod
- register the implementation class with the base class defined as the interface
There's a great article on this pymotw.
Thanks.
Virgilio De Vera
Still looking for help? Get the right answer, fast.
OR
Find an Online Tutor Now
Choose an expert and meet online.
No packages or subscriptions, pay only for the time you need.