python 将实例字符串转换为实例?
python convert string of instance to instance?
我有什么方法可以转换 python 实例的字符串:
"<__main__.test instance at 0x0325E5D0>"
实际实例
<__main__.test instance at 0x0325E5D0>
虽然保留了与作为实际实例时相同的数据,但我一直无法找到类似于 instance() 函数的东西,其作用类似于 str() 或 int()
我试过使用此处显示的建议函数:
但它不起作用
非常感谢您的建议
您可能想要的是序列化以存储您的对象供以后使用。
保存起来
import pickle
your_class = ...
with open('my_instance.pickle', 'wb') as handle:
pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)
待加载
import pickle
with open('my_instance.pickle', 'rb') as handle:
b = pickle.load(handle)
我有什么方法可以转换 python 实例的字符串:
"<__main__.test instance at 0x0325E5D0>"
实际实例
<__main__.test instance at 0x0325E5D0>
虽然保留了与作为实际实例时相同的数据,但我一直无法找到类似于 instance() 函数的东西,其作用类似于 str() 或 int()
我试过使用此处显示的建议函数:
非常感谢您的建议
您可能想要的是序列化以存储您的对象供以后使用。
保存起来
import pickle
your_class = ...
with open('my_instance.pickle', 'wb') as handle:
pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)
待加载
import pickle
with open('my_instance.pickle', 'rb') as handle:
b = pickle.load(handle)