How to copy an instance without its cStringIO.StringO item ?

Gabriel Genellina gagsl-py2 at
Sun Feb 8 20:09:51 CET 2009

En Sun, 08 Feb 2009 14:17:34 -0200, Barak, Ron <Ron.Barak at>  

> I need to copy an instance of a class, where one of the items in the  
> original is a cStringIO.StringO object.
> I do not need the cStringIO.StringO in the target object.
> The target instance is to be pickled.
> [...]
>             for_pickle_log_stream = LogStream(sac_log_file)
>             for key in log_stream.__dict__:
>                 if key != "input_file":
>                     for_pickle_log_stream[key] = log_stream[key]

Note that you're iterating over __dict__ keys, but the last line does not  
use __dict__. Either put __dict__ everywhere, or:
     setattr(for_pickle_log_stream, key, getattr(log_stream, key))

But if you want this copy just to pickle it, follow Christian Heimes  
advice and define __getstate__ / __setstate__.

Gabriel Genellina

More information about the Python-list mailing list