[EMAIL PROTECTED] wrote:
> Hi,
> I am new in Python World.I want to know what is mean by ``pickling''
> and ``unpickling'' ?
> And how can we used it?Please Give Me some links of Picking Examples.
> Thanks
You can generally answer such questions yourself
In article <[EMAIL PROTECTED]>,
"Lonnie Princehouse" <[EMAIL PROTECTED]> wrote:
> Pickling is the Python term for serialization. See
> http://en.wikipedia.org/wiki/Serialization
>
> Suppose you want to save a Python object "x" to a file...
>
> output_file = open('my_pickle', 'wb') # open a fi
Pickling is the Python term for serialization. See
http://en.wikipedia.org/wiki/Serialization
Suppose you want to save a Python object "x" to a file...
output_file = open('my_pickle', 'wb') # open a file
import pickle
pickle.dump(x, output_file) # write x to the file
output_file.close()
...
Hi,
I am new in Python World.I want to know what is mean by ``pickling''
and ``unpickling'' ?
And how can we used it?Please Give Me some links of Picking Examples.
Thanks
Sushant
--
http://mail.python.org/mailman/listinfo/python-list
> >
> OK, but do be aware that slots was intended solely as a
> memory-conservation measure where large numbers of objects are being
> created. You are therefore subverting its true intent by using it to
> limit the assignment of extra attributes (and there has been much recent
> discussion on thi
Alex wrote:
> Thanks to both Alex Martelli and Steve Holden.. We need __slots__ for
> other reasons, too long to explain, but basically to prevent assignment
> of extra attributes.
>
> I ended up changing child classes this way:
>
> def __getstate__(self):
> return(Parent.__getstate__
Thanks to both Alex Martelli and Steve Holden.. We need __slots__ for
other reasons, too long to explain, but basically to prevent assignment
of extra attributes.
I ended up changing child classes this way:
def __getstate__(self):
return(Parent.__getstate__(self), self.C)
def __se
, 'w')
>>>>cPickle.dump(obj, File)
>>>>File.close()
>>>>
>>>>file1=open('test', 'r')
>>>>objct=cPickle.load(file1)
>>>>file1.close()
>>>>objct.A
>
>
> Traceback (most recent call l
Alex <[EMAIL PROTECTED]> wrote:
> I have a serious problem and I hope there is some solution. It is
> easier to illustrate with a simple code:
>
> >>> class Parent(object):
> __slots__=['A', 'B']
> def __init__(self, a, b):
> self.A=a; self.B=b
> def __getstate__(s
Thanks so much! It makes perfect sense and I am sure it will work now.
--
http://mail.python.org/mailman/listinfo/python-list
Of course, in __setstate__, there should be a tup = list(tup) as well -
sheer forgetfulness and a confusing name in one.
--
http://mail.python.org/mailman/listinfo/python-list
The reason the state of obj.A and obj.B aren't preserved is because
your __getstate__ and __setstate__ don't preserve them - they only save
obj.C, and you don't make a call to the parent class's respective
methods. Here's what I mean:
>>> import pickle
>>> class Child(Parent):
__slots__=[
Sorry, I copied and pasted a wrong piece from shell at one part of the
code:
objct.Z=4 was in fact obj.Z=4 and it did refuse to accept Z (because it
is not in __slots__).
But the question remains: why the value of attribute A is not preserved
during pickling and unpickling and what can be done
File "", line 1, in -toplevel-
objct.A
AttributeError: A
>>> objct.C
1
Its own attribute (C) value is restored but the value of an inherited
attribute (A) is not. I tried pickling protocol 2, and module pickle
instead of cPickle, all with the same result.
What can be done?! Or maybe nothing can be done?
I would greatly appreciate an advice. A lot of code is written, but now
we have a need for pickling and unpickling objects and discovered this
problem.
--
http://mail.python.org/mailman/listinfo/python-list
14 matches
Mail list logo