I'm trying to use the shelve module in python, and I'm trying to combine it with the "with" statement, but when trying to do that I get the following error:
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)
The following error is raised:
with shelve.open('shelve', 'c') as shlv_file:
AttributeError: DbfilenameShelf instance has no attribute '__exit__'
Although doing this way:
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)
No error is raised, and the output is the one that is expected. What is the problem with the first syntax ? I was watching a python course in which the instructor uses the first version without any problems.