Python Metaclass : Understanding the 'with_metaclass()'

后端 未结 2 1992
情深已故
情深已故 2020-12-24 01:33

I want to ask what the with_metaclass() call means in the definition of a class.

E.g.:

class Foo(with_metaclass(Cls1, Cls2)):

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 02:19

    UPDATE: the six.with_metaclass() function has since been patched with a decorator variant, i.e. @six.add_metaclass(). This update fixes some mro issues related to the base objects. The new decorator would be applied as follows:

    import six
    
    @six.add_metaclass(Meta)
    class MyClass(Base):
        pass
    

    Here are the patch notes and here is a similar, detailed example and explanation for using a decorator alternative.

提交回复
热议问题