代碼如下
# 將class轉dict,以_開頭的屬性不要 def props(obj): pr = {} for name in dir(obj): value = getattr(obj, name) if not name.startswith('__') and not callable(value) and not name.startswith('_'): pr[name] = value return pr # 將class轉dict,以_開頭的也要 def props_with_(obj): pr = {} for name in dir(obj): value = getattr(obj, name) if not name.startswith('__') and not callable(value): pr[name] = value return pr # dict轉obj,先初始化一個obj def dict2obj(obj,dict): obj.__dict__.update(dict) return obj