Don't want to serialize a variable of object

Tarek Ziadé tziade at
Tue Oct 18 09:30:49 CEST 2005

Iyer, Prasad C wrote:

>I got a class which I need to serialize, except for couple of variable.
>import cPickle as p
>class Color:
>    def __init__(self):
>        print "hello world"
>        self.x=10
>        self.somechar="this are the characters"
>f=file('poem.txt', 'w')
>p.dump(color, f)
>How do I serialize the object color without serializing the x and
>somechar variables?
>Is there any modifier which prevents the variable from being serialized.
You can create your own serialization process, by providing some kind of 
serialize() method
 in a base class that would dump part of the object.

Let's say, the instance __dict__ dictionnary ?
this serialize method can then remove the attributes that starts with '_v_'
(that's what we do in Zope to avoid pickling some attribute)

>Another question:
>	Is there a concept of private variables?
That's a long discussion, you should look at the archives in the list.

But anyway, all attributes that starts with '__' are considered private 
to the class.
(ie: can't be reached by "instance.__attribute")

even though you can find it if you dig into instance.__dict__



More information about the Python-list mailing list