[Tutor] Importing global variables and classes from other modules

Daniel Ehrenberg littledanehren at yahoo.com
Mon Dec 22 15:05:05 EST 2003


Venkatesh Babu wrote:
> Hi Danny,
> 
> My program will be creating classes dynamically and
> I wish to save those
> class definitions into the file and retrive these
> definitions at some
> other point in time. I first thought of a simple
> strategy of storing the
> class definition in file as a string and later
> executing the string so
> that the class is created. But the class gets
> created inside the function
> in which i call exec and its scope is confined to
> that function, so I
> thought of using import instead of executing the
> string.
> 
> Can u suggest any other strategy for my problem.
> 
> Thank you,
> Venkatesh

Try using the pickle module. It lets you easily store
any datatype (including classes) in a file or string.
If your class is dependent on a module (like
inheringing from a module or something), pickle will
handle everything for you, automatically.

Daniel Ehrenberg 

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/



More information about the Tutor mailing list