Python: How do I make a subclass from a superclass?

后端 未结 11 1836
日久生厌
日久生厌 2020-11-29 19:01

In Python, how do you make a subclass from a superclass?

11条回答
  •  一个人的身影
    2020-11-29 19:35

    There is another way to make subclasses in python dynamically with a function type():

    SubClass = type('SubClass', (BaseClass,), {'set_x': set_x})  # Methods can be set, including __init__()
    

    You usually want to use this method when working with metaclasses. When you want to do some lower level automations, that alters way how python creates class. Most likely you will not ever need to do it in this way, but when you do, than you already will know what you are doing.

提交回复
热议问题