Python和泡菜。腌制实例失去很多争论

时间:2018-12-30 16:47:25

标签: python pickle

如何正确定义一个类,以免在酸洗过程中丢失参数?

import pickle
class test(object):
    def __init__(self, **kwargs):
        self.name=kwargs.get("name")
        self.age=kwargs.get("age")
        self.height = kwargs.get("height")


instance=test(name="Adrian", age=15, height="165")

pickled = pickle.dumps(instance)

unpickled = pickle.loads(pickled)

print (dir(instance))       
print (dir(unpickled))

我的代码比使用很多kwargs的代码大一点,并且在酸洗过程中,在取消酸洗的过程中会倾倒大量我的参数

这是dir(instance)之前和之后的实际值

之前

'__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'button_offset_mult', 'drag_callback_local_lighting_manager_window', 'exposure_slide_change_value', 'light_preview_color_temp', 'light_preview_hdri_color', 'light_preview_hdri_path', 'light_preview_hdri_preview_path', 'light_preview_light_samples', 'light_preview_lightname', 'light_preview_set_parent', 'light_preview_size_height', 'light_preview_type', 'light_preview_use_color_temp', 'light_uses_hdr_boo', 'lightools_change_hdr_to', 'lighttools_light_on_off', 'slider_range_force_change_manu'

之后

'__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'button_offset_mult', 'color_swatch_button', 'drag_callback_local_lighting_manager_window', 'expo_exposure_floatField', 'expo_slider_per_light', 'expo_slider_per_light_value_max', 'expo_slider_per_light_value_min', 'expo_slider_per_light_value_value', 'exposure_slide_change_value', 'hdr_preview_pane', 'light_preview_color_temp', 'light_preview_hdri_color', 'light_preview_hdri_path', 'light_preview_hdri_preview_path', 'light_preview_light_samples', 'light_preview_lightname', 'light_preview_set_parent', 'light_preview_size_height', 'light_preview_type', 'light_preview_use_color_temp', 'light_uses_hdr_boo', 'lightools_change_hdr_to', 'lighttools_light_on_off', 'master_light_thumbnail_layout', 'menu_item_hdri_break', 'menu_item_hdri_color_change', 'popup_menu_hdriswatch', 'preview_display_icon_for_light_type', 'preview_display_lightname_display', 'preview_display_within', 'preview_right_click', 'slider_range_force_change_manu', 'turn_off_on_button'

在阅读时,我必须设置 repr reduce 函数以返回正确的参数,但是我不知道如何使用很多方法怪人?

我是否必须分别设置每个参数,或者有更有效的方法吗?

0 个答案:

没有答案