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

前端 未结 6 1616
醉酒成梦
醉酒成梦 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:53

    Pickle only works with classes defined in module scope (top level). In this case, it looks like you could define the nested classes in module scope and then set them as properties on WidgetType, assuming there's a reason not to just reference TextType and FloatType in your code. Or, import the module they're in and use widget_type.TextType and widget_type.FloatType.

提交回复
热议问题