Say I have defined a class myself and I defined a __repr__
method for it. I want to about convert it back to my object. I know that object serialization may be a good way of doing so (using the json
module) but is there anyway I can use the built-in eval
function to achieve this?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Write your
__repr__()
so it creates a valid Python expression for instantiating your object.Obviously this relies on the values you're using having their own reasonable
repr()
.You don't have to define any special
eval()
—just pass in whatever you get fromrepr()
.