For this example, I have a dictionary, that when I call on it, “Ember Attack” is displayed.
#import shelve
class Pokemon():
"""Each pokemon's attributes"""
def __init__(self):
self.id=[]
self.var1=[]
self.var2=[]
self.var3=[]
self.var4=[]
self.var5=[]
def __str__(self):
showList=['id','var1', 'var2', 'var3', 'var4', 'var5']
#dict1=shelve.open("shelve.dat")
dict1={}
dict1["Charmander"]=Pokemon()
dict1["Charmander"].var1="Ember Attack"
#dict1.sync()
print dict1["Charmander"].var1
#dict1.close()
However when I start using shelves instead of the dictionary, I get a blank when I call on var1.
import shelve
class Pokemon():
"""Each patient's attributes"""
def __init__(self):
self.id=[]
self.var1=[]
self.var2=[]
self.var3=[]
self.var4=[]
self.var5=[]
def __str__(self):
showList=['id','var1', 'var2', 'var3', 'var4', 'var5']
dict1=shelve.open("shelve.dat")
#dict1={}
dict1["Charmander"]=Pokemon()
dict1["Charmander"].var1="Ember Attack"
dict1.sync()
print dict1["Charmander"].var1
dict1.close()
The only difference is that I made dict1 a shelve dictionary instead of a regular dictionary. It probably has to do with memory scope or something. Anyway, can someone help me revise my code so that it will work with shelves? Thanks!
you can also specify the protocol which should improve performance