|
|
|
|
|
by dsj36
4548 days ago
|
|
I think it was David Beazley who characterized metaclasses as "infecting" an inheritance tree. If `class A` has a metaclass, then all subclasses of `A` will inherit the metaclass as well. This behavior is not shared by class decorators, which only affect the decorated class. So, I think this is a pretty reasonable place to use them -- getting magic behavior from class decorators / metaclasses is bad enough, but getting surprised when losing it upon subclassing is even worse! |
|
Metaclasses (and build_class) is a mechanism by which to enforce a constraint from a base type to a derived type.
(Note that, in practice, there is some trickiness around metaclasses on derived classes: http://seriously.dontusethiscode.com/2013/04/18/derived-meta...)
It's trivial to enforce a constraint from a derived type to a base type. e.g.,
But how can we enforce a constraint in the other direction? (e.g., abc.ABCMeta)