How can I pickle a dynamically created nested class in python?

前端 未结 6 1629
醉酒成梦
醉酒成梦 2020-11-28 05:25

I have a nested class:

class WidgetType(object):
    
    class FloatType(object):
        pass
    
    class TextType(object):
        pass

.. and

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 05:49

    Nadia's answer is pretty complete - it is practically not something you want to be doing; are you sure you can't use inheritance in WidgetTypes instead of nested classes?

    The only reason to use nested classes is to encapsulate classes working together closely, your specific example looks like an immediate inheritance candidate to me - there is no benefit in nesting WidgetType classes together; put them in a module and inherit from the base WidgetType instead.

提交回复
热议问题