English 中文(简体)
how to use pickle
原标题:

my code(i was unable to use pickle ):

class A(object):
    def __getstate__(self):
        print  www 
        return  sss 
    def __setstate__(self,d):
        print  aaaa 

import pickle
a = A()
s = pickle.dumps(a)
e = pickle.loads(s)
print s,e

print :

www
aaaa
ccopy_reg
_reconstructor
p0
(c__main__
A
p1
c__builtin__
object
p2
Ntp3
Rp4
S sss 
p5
b. <__main__.A object at 0x00B08CF0>

who can tell me how to use.

最佳回答

What are you trying to do? It works for me:

class A(object):
    def __init__(self):
        self.val = 100

    def __str__(self):
        """What a looks like if your print it"""
        return  A: +str(self.val)

import pickle
a = A()
a_pickled = pickle.dumps(a)
a.val = 200
a2 = pickle.loads(a_pickled)
print  the original a 
print a
print # newline
print  a2 - a clone of a before we changed the value 
print a2
print 

print  Why are you trying to use __setstate__, not __init__? 
print

So this will print:

the original a
A:200

a2 - a clone of a before we changed the value
A:100

If you need setstate:

class B(object):
    def __init__(self):
        print  Perhaps __init__ must not happen twice? 
        print
        self.val = 100

    def __str__(self):
        """What a looks like if your print it"""
        return  B: +str(self.val)

    def __getstate__(self):
        return self.val

    def __setstate__(self,val):
        self.val = val

b = B()
b_pickled = pickle.dumps(b)
b.val = 200
b2 = pickle.loads(b_pickled)
print  the original b 
print b
print # newline
print  b2 - b clone of b before we changed the value 
print b2

which prints:

Why are you trying to use __setstate__, not __init__?

Perhaps __init__ must not happen twice?

the original b
B:200

b2 - b clone of b before we changed the value
B:100
问题回答

You are able to pickle (meaning, this code works as it should). You just seem to get a result, you don t expect. If you expect the same output , try:

import pickle
a = A()
s = pickle.dumps(a)
e = pickle.loads(s)
print s, pickle.dumps(e)

Your example isn t, well, a typical pickling example. Usually pickled objects are saved somewhere persistently or sent over the wire. See e.g. pickletest.py: http://www.sthurlow.com/python/lesson10/.

There are advanced uses of pickling, see for example David Mertz XML object serialisation article: http://www.ibm.com/developerworks/xml/library/x-matters11.html

In a nutshell, in your example, e equals a.

Don t have to care about these strang strings, you can dumps these strings to save to anywhere, just remember when you loads them, you got a object again.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签