Using Pavel Anossov's (now deleted) comment:
class met(type):
def __init__(cls, name, bases, dct):
super(met, cls).__init__(name, bases, dct)
cls._test_member = "test_value"
object = met('object', (object,), {})
class A(object):
pass
print(A._test_member)
prints
test_value
Note that a class can have only one metaclass. (Since, after all, any object can have only one type). But additionally, the metaclass of a class must be a (non-strict) subclass of the metaclasses of all its bases. In other words, the class's metaclass and the metaclasses of all its bases must either be the same, or all those metaclasses must be subclasses of each other. So the above solution may not work if a class tries to use a metaclass which is not a subclass of met
.
For example,
class met(type):
def __init__(cls, name, bases, dct):
super(met, cls).__init__(name, bases, dct)
cls._test_member = "test_value"
object = met('object', (object,), {})
class someothertype(type): pass
class B(object):
__metaclass__ = someothertype
raises
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases