pickling and methods

gods1child alankarmisra at hotmail.com
Wed Jul 4 17:54:44 EDT 2001


Hi,

When an object instance is pickled, is the byte-code associated with
the object's methods stored with it? Or does Python pickle only the
object type and state information ? for example if i have:
class Class:
    def function():
       self.stateinfo = 'mystate'
       pass # large amount of code here

a = Class()
b = Class()

If i pickled a and b, would they both have the byte-code for the
method 'function' or would they only have the class (Class) and the
state information (stateinfo)?

The reason for asking this is because I have a class which has a large
amount of code in its __init__ method. Once the object is created,
most of that code is pretty much useless. Many instances of this
object will be pickled during program execution. If Python does store
the function byte code with each instance of the object, I wanted to
consider the space saving i might achieve by moving most of the
processing code to a object-factory class instead which would return a
light weight class which i can pickle.

Even from a good programming perspective, i wanted to know your
opinions of moving the __init__ code to a factory method in cases
where the processing code is huge and is never used (not even while
unpickling) once the object is instantiated.

Thanx!



More information about the Python-list mailing list