How do I reverse str(class) in Python?
Question: I need to define a new instance of a class from an informal string
representation of the same type of class. What is a clean way to do this in
Python?

program1.py:

    
    
    fileHandle = open("filename.txt", "wb")
    instance = className()
    instance.a = 2
    instance.b = 3.456
    fileHandle.write(str(instance))
    

filename.txt (after running program1.py):

    
    
    <className a=2, b=3.456>
    

program2.py:

    
    
    instance = className()
    with open("filename.txt", "r") as fileHandle:
        for fileLine in fileHandle:
            ##### How do I grab the contents of the file line and get them in variables? #####
            (instance.a, instance.b) = magicFunction(fileLine)
            # I seem to have forgotten the contents of magicFunction(). Can someone remind me?
    


Answer: In general, the python `str` function is designed for printing things in a
**human-readable** way, not computer-readable as you want to use. If you
control `program1.py`, the `pickle` module could fit your needs.

    
    
    Program1.py:
        import pickle
        [rest of code unrelated to printing]
        pickle.dump(instance,fileHandle)
    Program2.py:
        instance = pickle.load(fileHandle)
    

