0

is there any way for me to convert a string of a python instance:

"<__main__.test instance at 0x0325E5D0>"

to an actual instance

<__main__.test instance at 0x0325E5D0>

while having keep the same data it had when being an actual instance, I haven't been able to find something like an instance() function that would act similar to str() or int()

i've tried using the suggested function shown here: PYTHON : There is a function similar to ast.literal_eval ()? but it doesn't work

advice would be much appreciated

6
  • 5
    No. This is impossible. Commented Aug 5, 2017 at 16:51
  • 1
    what is it that you are trying to achieve by doing this ? Commented Aug 5, 2017 at 16:55
  • 3
    It's like trying to turn the street address of a building back into the building that used to be there after the whole city has been razed to the ground. Commented Aug 5, 2017 at 16:56
  • 2
    Use the pickle module. (as I said in my answer to the proposed duplicate)' Commented Aug 5, 2017 at 16:56
  • 1
    If the object still exists at that address, then there are ways (but it's not recommended). Commented Aug 5, 2017 at 16:58

1 Answer 1

0

What you maybe want is serialization to store your object for later use.

To save it

import pickle

your_class = ...

with open('my_instance.pickle', 'wb') as handle:
    pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)

To load

import pickle
with open('my_instance.pickle', 'rb') as handle:
    b = pickle.load(handle)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.