[Tutor] how to shelve classes
Payal
payal-python at scriptkitchen.com
Fri Jun 18 12:22:26 CEST 2010
Hi,
I want to carry my classes around, so I wrote foo.py as,
#!/usr/bin/python
import pickle, shelve
f = shelve.open('movies')
class F(object) :
def __init__(self, amt) : self.amt = amt
def dis(self) : print 'Amount : ', self.amt
def add(self, na) :
self.amt += na
F.dis(self)
f['k'] = F
x=F(99)
f['k2']=x
Now in python interpreter I get,
>>> import shelve
>>> f = shelve.open('movies')
>>> F2=f['k']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/shelve.py", line 122, in __getitem__
value = Unpickler(f).load()
AttributeError: 'module' object has no attribute 'F'
How do I carry around my classes and instances?
With warm regards,
-Payal
--
More information about the Tutor
mailing list