您的位置:首页 > 其它

为什么shelve会失败?

2017-10-16 22:29 127 查看
先看代码:

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'

这是为什么?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: