Is there a way to set metaclass after the class definition?

后端 未结 5 1907
独厮守ぢ
独厮守ぢ 2020-12-15 08:16

In order to set metaclass of a class, we use the __metaclass__ attribute. Metaclasses are used at the time the class is defined, so setting it explicitly after

5条回答
  •  离开以前
    2020-12-15 08:40

    I am unable to test this out right now (I don't have access to Python in my machine right now :-(), but unless metaclass is imutable, perhaps you can try something like this:

    >>> class B:
            pass
    >>> setattr(B, "__metaclass__", MetaClass)
    

    Again, I can't test it right now so I apologize if this is an invalid answer, just trying to help. :)

提交回复
热议问题