Have a look at abc module. For 2.7: link. For 3.6: link
Simple example for you:
from abc import ABC, abstractmethod
class A(ABC):
def __init__(self, value):
self.value = value
super().__init__()
@abstractmethod
def do_something(self):
pass
class B(A):
@abstractmethod
def do_something_else(self):
pass
class C(B):
def do_something(self):
pass
def do_something_else(self):
pass
Answer from luminousmen on Stack OverflowHave a look at abc module. For 2.7: link. For 3.6: link
Simple example for you:
from abc import ABC, abstractmethod
class A(ABC):
def __init__(self, value):
self.value = value
super().__init__()
@abstractmethod
def do_something(self):
pass
class B(A):
@abstractmethod
def do_something_else(self):
pass
class C(B):
def do_something(self):
pass
def do_something_else(self):
pass
from abc import ABCMeta, abstractmethod
class OriginalAbstractclass(metaclass=ABCMeta):
@abstractmethod
def sample_method(self):
pass
class InheritedAbstractClass(OriginalAbstractclass, metaclass=ABCMeta):
@abstractmethod
def another_method(self):
pass
class ConcreteClass(InheritedAbstractClass):
def some_method(self):
pass
def another_method(self):
pass
inheritance - Abstract methods in Python - Stack Overflow
object oriented - Abstract base classes and mix-ins in python - Software Engineering Stack Exchange
Should we use an abstract class when we know a parent class should never be directly accessed?
How to create abstract properties in python abstract classes? - Stack Overflow
Videos
Before abc was introduced you would see this frequently.
class Base(object):
def go(self):
raise NotImplementedError("Please Implement this method")
class Specialized(Base):
def go(self):
print "Consider me implemented"
Something along these lines, using ABC
import abc
class Shape(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def method_to_implement(self, input):
"""Method documentation"""
return
Also read this good tutorial: https://pymotw.com/3/abc/
You can also check out zope.interface which was used prior to introduction of ABC in python.
- http://pypi.python.org/pypi/zope.interface
- https://zopeinterface.readthedocs.io/en/latest/README.html
I'm attempting to code something to perform a computation and I'm a bit unsure on how to handle inheriting. The idea is that there are a few "compute" classes that each do something a bit different from one another, but nevertheless share some underlying functionality. So I have a "parent" class that they each inherit from. However, there will never be a reason to directly access the parent class though. Should I be using an abstract class? I think these might be relevant since they are used to mix into the classes but aren't directly accessed. Although I don't know if this is really necessary or the best way to go about this.
Since Python 3.3 a bug was fixed meaning the property() decorator is now correctly identified as abstract when applied to an abstract method.
Note: Order matters, you have to use @property above @abstractmethod
Python 3.3+: (python docs):
from abc import ABC, abstractmethod
class C(ABC):
@property
@abstractmethod
def my_abstract_property(self):
...
Python 2: (python docs)
from abc import ABCMeta, abstractproperty
class C:
__metaclass__ = ABCMeta
@abstractproperty
def my_abstract_property(self):
...
Until Python 3.3, you cannot nest @abstractmethod and @property.
Use @abstractproperty to create abstract properties (docs).
from abc import ABCMeta, abstractmethod, abstractproperty
class Base(object):
# ...
@abstractproperty
def name(self):
pass
The code now raises the correct exception:
Traceback (most recent call last):
File "foo.py", line 36, in
b1 = Base_1('abc')
TypeError: Can't instantiate abstract class Base_1 with abstract methods name
Use the abc module to create abstract classes. Use the abstractmethod decorator to declare a method abstract, and declare a class abstract using one of three ways, depending upon your Python version.
In Python 3.4 and above, you can inherit from ABC. In earlier versions of Python, you need to specify your class's metaclass as ABCMeta. Specifying the metaclass has different syntax in Python 3 and Python 2. The three possibilities are shown below:
Copy# Python 3.4+
from abc import ABC, abstractmethod
class Abstract(ABC):
@abstractmethod
def foo(self):
pass
Copy# Python 3.0+
from abc import ABCMeta, abstractmethod
class Abstract(metaclass=ABCMeta):
@abstractmethod
def foo(self):
pass
Copy# Python 2
from abc import ABCMeta, abstractmethod
class Abstract:
__metaclass__ = ABCMeta
@abstractmethod
def foo(self):
pass
Whichever way you use, you won't be able to instantiate an abstract class that has abstract methods, but will be able to instantiate a subclass that provides concrete definitions of those methods:
>>> Abstract()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Abstract with abstract methods foo
>>> class StillAbstract(Abstract):
... pass
...
>>> StillAbstract()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class StillAbstract with abstract methods foo
>>> class Concrete(Abstract):
... def foo(self):
... print('Hello, World')
...
>>> Concrete()
<__main__.Concrete object at 0x7fc935d28898>
The old-school (pre-PEP 3119) way to do this is just to raise NotImplementedError in the abstract class when an abstract method is called.
Copyclass Abstract(object):
def foo(self):
raise NotImplementedError('subclasses must override foo()!')
class Derived(Abstract):
def foo(self):
print 'Hooray!'
>>> d = Derived()
>>> d.foo()
Hooray!
>>> a = Abstract()
>>> a.foo()
Traceback (most recent call last): [...]
This doesn't have the same nice properties as using the abc module does. You can still instantiate the abstract base class itself, and you won't find your mistake until you call the abstract method at runtime.
But if you're dealing with a small set of simple classes, maybe with just a few abstract methods, this approach is a little easier than trying to wade through the abc documentation.