Answer the question
In order to leave comments, you need to log in
Why doesn't the Shelve module work in Python 3?
Good afternoon. There is a piece of code:
import shelve
data = shelve.open("quiz")
data["quiz1"] = {"theme" : None}
data["quiz1"]["theme"] = "Cinematograph"
print(data["quiz1"]["theme"])
data.close()
Answer the question
In order to leave comments, you need to log in
Look. data is a file handle that mimics the behavior of a dictionary. It has a __setattr__ method that is called directly when you write the "square brackets" operator with equality. In your case:
data['key'] = {'this': 'is dict'}
The datatype of data is shelve
The datatype of data['key'] is a dictionary
When you write this:
data["quiz1"][" theme"] = "Cinematograph"
the __setattr__ method is called on the data["quiz1"] dictionary
and it has nothing to do with shelve. Therefore, in order to do what you want, you need to rewrite it somehow like this:
import shelve
data = shelve.open("quiz")
data["quiz1"] = {"theme" : None}
tmp = data["quiz1"]
tmp["theme"] = "Cinematograph"
data["quiz1"] = tmp
print(data["quiz1"]["theme"])
data.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question