How to make type cast for python custom class
问题 Consider a class; class temp: def __init__(self): pass I make an object of it; obj = temp() Convert it to string; strObj = str(obj) Now, how can i convert strObj to an object of temp class?? org = temp(strObj) 回答1: As noted by Anand in comments, what you're looking for is object serialization and deserialization. One way to achieve this is through the pickle (or cPickle) module: >>> import pickle >>> class Example(): ... def __init__(self, x): ... self.x = x ... >>> a = Example('foo') >>>