def function:
x=100
var1=type("Myclass",(),{"fucntion":function})
print(var1)
The type()
function will generate a class of name Myclass
and now I can call it from a object but now I want to save the state of the program.To simply put Myclass
is generated when type() function runs and I want somehow when I again run the program Myclass
should already be declared without running type() function.Its like a code that changes itself and saves itself and when again launched runs from the last saved state.
You need to look into pickling. To save a variable:
import pickle
def function():
x=100
var1=type("Myclass",(),{"fucntion":function})
outfile = open(filename,'wb')
pickle.dump(var1,outfile)
outfile.close()
print(var1)
To get the variable:
infile = open(filename,'rb')
Myclass = pickle.load(infile)
infile.close()