In Python 3 they're written like
from abc import ABCMeta
class MyAbstractBaseClass(metaclass=ABCMeta):
@abstractmethod
def foo():
pass
in Python 2 they're written like
from abc import ABCMeta
class MyAbstractBaseClass(object):
__metaclass__ = ABCMeta
@abstractmethod
def foo():
pass
But the metaclass=ABCMeta
from Python 3 causes a SyntaxError
in Python 2, and the __metaclass__ = ABCMeta
line from Python 2 has no effect in Python 3 (it's possible to instantiate a subclass without those abstract methods defined).
So is there a way of doing this in both Python 2.x and 3.x?