In Python 3.4+, we can do
class Foo(abc.ABC): ...
or we can do
class Foo(metaclass=abc.ABCMeta): ...
Are there any differences between the two that I should be aware of?
abc.ABC is basically just an extra layer on top of metaclass=abc.ABCMeta . ie abc.ABC implicitly defines a metaclass for us.
abc.ABC
metaclass=abc.ABCMeta
(Source: https://hg.python.org/cpython/file/3.4/Lib/abc.py#l234 )
class ABC(metaclass=ABCMeta): """Helper class that provides a standard way to create an ABC using inheritance. """ pass
The only difference is that in the first case you need simple inheritance, and in the last you need to specify a metaclass.
From What's New in Python 3.4 (highlighted by me):
The new ABC class has ABCMeta as its metaclass. Using ABC as a base class has the same effect as specifying metaclass=abc.ABCMeta , but it's easier to type and read easier .
ABC
ABCMeta
Related problem: Create abstract base classes by inheritance, not direct call __metaclass__
__metaclass__