搁置模块不能与"使用"声明

时间:2017-08-20 22:41:10

标签: python persistence shelve

我试图在python中使用shelve模块,我试图将它与"和"结合使用。声明,但在尝试这样做时,我收到以下错误:

with shelve.open('shelve', 'c') as shlv_file:
    shlv_file['title']    = 'The main title of my app'
    shlv_file['datatype'] = 'data type int32'
    shlv_file['content']  = 'Lorem ipsum'
    shlv_file['refs']     = '<htppsa: asda.com>'

print(shlv_file)

引发以下错误:

 with shelve.open('shelve', 'c') as shlv_file:
AttributeError: DbfilenameShelf instance has no attribute '__exit__'

虽然这样做:

shlv_file = shelve.open('data/shelve', 'c')
shlv_file['title']    = 'The main title of my app'
shlv_file['datatype'] = 'data type int32'
shlv_file['content']  = 'Lorem ipsum'
shlv_file['refs']     = '<htppsa: asda.com>'
shlv_file.close()

shlv_file = shelve.open('data/shelve', 'c')
shlv_file['new_filed'] = 'bla bla bla'
print(shlv_file)

不会引发错误,输出是预期的输出。第一种语法有什么问题?我正在观看一个python课程,教师使用第一个版本没有任何问题。

1 个答案:

答案 0 :(得分:3)

您需要了解with的用途是什么。它基本上用于自动处理调用对象的设置和清理,前提是这些对象支持使用此类设置和清理功能。特别是,用于设置的对象是__enter__函数,而拆除等效函数是__exit__函数。这是一个例子:

In [355]: class Foo():
     ...:     def __enter__(self):
     ...:         print("Start!")
     ...:         return self
     ...:     def __exit__(self, type, value, traceback):
     ...:         print("End!")
     ...:         

现在,使用Foo实例化with...as的对象:

In [356]: with Foo() as x:
     ...:     print(x)
     ...:     
Start!
<__main__.Foo object at 0x1097f5da0>
End!

如您所见,with...as将强制调用这些设置/拆卸方法,如果它们丢失,将引发AttributeError因为尝试调用不存在的实例方法是。

与您的shelve对象相同 - 它没有在其类中定义__exit__方法,因此使用with将无效。< / p>

根据documentation,从3.4及更高版本添加了对上下文管理器的支持。如果上下文管理器不起作用,那就意味着您的版本较旧。