先看代码:
class Empty:
pass
def save_obj():
x = Empty
x.name='hello'
x.value = 343
with shelve.open('empty') as db:
db['x']=x
def load_obj():
with shelve.open('empty') as db:
x = db['x']
print(x.name, x.value)
当我第一次运行:
save_obj()
load_obj()
结果正常显示。
但是第二次运行
load_obj()
却抛出异常:
AttributeError: type object 'Empty' has no attribute 'name'
这是为什么?