In Python 2, you should assign a metaclass as follows:
import abc class ABC(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def foo(self): return True a = ABC()
What causes a TypeError
Traceback (most recent call last): File "<pyshell#59>", line 1, in <module> a = ABC() TypeError: Can't instantiate abstract class ABC with abstract methods foo
But in Python 3, assigning __metaclass__ as an attribute does not work (as you intend it, but the interpreter does not consider it an error, and the regular attribute is like any other, so the code above will not lead to an error). Metaclasses are now defined as a named argument to the class:
import abc class ABC(metaclass=abc.ABCMeta): @abc.abstractmethod def foo(self): return True a = ABC()
raises a TypeError :
Traceback (most recent call last): File "main.py", line 11, in a = ABC() TypeError: Can't instantiate abstract class ABC with abstract methods foo
source share