Pickle performing class instantiation ??

Smiley 4321 ssmile03 at gmail.com
Tue Feb 28 16:54:35 CET 2012


Am I doing the right thing for -

- Have a class
- Instantiate the class with 3 parameters
- pickle the class instance
- generate a bytestream (.pkl) using pickle.dump, simply guessing -

as -

---
#!/usr/bin/python

import pickle

class Pickle:
    def __init__(self, Parameter1, Parameter2, Parameter3):
        self.PM1 = Parameter1
        self.PM2 = Parameter2
        self.PM3 = Parameter3

with open('/tmp/readfile.pkl', 'wb') as f:
    pickle.dump((self.PM1, self.PM2, self.PM3), f)

with open('/tmp/readfile.pkl', 'rb') as f:
    self.PM1, self.PM2, self.PM3 = pickle.load(f)

print self.PM1
print self.PM2
print self.PM3
----

but I get an error message as -

-----
$ ./class-test1.py
Traceback (most recent call last):
  File "./class-test1.py", line 12, in <module>
    pickle.dump((self.PM1, self.PM2, self.PM3), f)
NameError: name 'self' is not defined
------

On Tue, Feb 28, 2012 at 8:44 PM, Smiley 4321 <ssmile03 at gmail.com> wrote:

> I am looking for pickle performing class instantiation, something as
> prototype like -
>
> - Have a class
> - Instantiate the class with 3 parameters
> - pickle the class instance
> - generate a bytestream (.pkl) using pickle.dump
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120228/bc5f7675/attachment.html>


More information about the Python-list mailing list