Is it possible to inheret a metaclass.

Antoon Pardon antoon.pardon at vub.be
Fri Apr 10 06:49:11 EDT 2020



Op 9/04/20 om 18:37 schreef Peter Otten:
> Antoon Pardon wrote:
> 
>> I am experimenting with subclasses that all need the same metaclass as the
>> base class. Is there a way to make the metaclass be inherited, so that you
>> don't have to repeat the "metaclass = MetaClass" with every subclass.
> 
> ?
> 
> This is not only possible, this is the default:
> 
>   >>> class Pardon(type): pass
> ...
>>>> class A(metaclass=Pardon): pass
> ...
>>>> class B(A): pass
> ...
>>>> type(B)
> <class '__main__.Pardon'>

Can you explain what is wrong with the code below:

This produces only: Calling Pardon with A.


def Pardon(cls, *args):
     print("Calling Pardon with", cls)
     return type(cls, *args)

class A(metaclass=Pardon): pass

class B(A): pass


More information about the Python-list mailing list