搁置模块不适用于 "with" 语句
shelve module does not work with "with" statement
我正在尝试使用 python 中的搁置模块,并试图将其与 "with" 语句结合使用,但在尝试这样做时出现以下错误:
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 课程,其中讲师使用第一个版本没有任何问题。
你需要明白with
的用途是什么。它基本上用于自动处理调用它的对象的设置和清理,前提是这些对象支持使用此类设置和清理功能。特别是,设置对象是 __enter__
函数,拆解等效对象是 __exit__
函数。这是一个例子:
In [355]: class Foo():
...: def __enter__(self):
...: print("Start!")
...: return self
...: def __exit__(self, type, value, traceback):
...: print("End!")
...:
并且,现在用 with...as
:
实例化 Foo
的对象
In [356]: with Foo() as x:
...: print(x)
...:
Start!
<__main__.Foo object at 0x1097f5da0>
End!
如您所见,with...as
将强制调用这些 setup/teardown 方法,如果它们缺失,则会引发 AttributeError
,因为试图调用不存在的实例方法制作完成。
它与您的 shelve
对象相同 - 它没有在其 class 中定义的 __exit__
方法,因此使用 with
将不起作用。
根据 documentation,从 3.4 版及更高版本添加了对上下文管理器的支持。如果上下文管理器不起作用,则意味着您的版本较旧。
我正在尝试使用 python 中的搁置模块,并试图将其与 "with" 语句结合使用,但在尝试这样做时出现以下错误:
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 课程,其中讲师使用第一个版本没有任何问题。
你需要明白with
的用途是什么。它基本上用于自动处理调用它的对象的设置和清理,前提是这些对象支持使用此类设置和清理功能。特别是,设置对象是 __enter__
函数,拆解等效对象是 __exit__
函数。这是一个例子:
In [355]: class Foo():
...: def __enter__(self):
...: print("Start!")
...: return self
...: def __exit__(self, type, value, traceback):
...: print("End!")
...:
并且,现在用 with...as
:
Foo
的对象
In [356]: with Foo() as x:
...: print(x)
...:
Start!
<__main__.Foo object at 0x1097f5da0>
End!
如您所见,with...as
将强制调用这些 setup/teardown 方法,如果它们缺失,则会引发 AttributeError
,因为试图调用不存在的实例方法制作完成。
它与您的 shelve
对象相同 - 它没有在其 class 中定义的 __exit__
方法,因此使用 with
将不起作用。
根据 documentation,从 3.4 版及更高版本添加了对上下文管理器的支持。如果上下文管理器不起作用,则意味着您的版本较旧。