1

I have a python code which uses some singleton class. One of the usecases I need is to store the class as a pickled object. The application logic controls when to read from the pickled object and when to rebuild it (something like caching).

The problem is that the object type/name is identical to the in memory object, and due to it, even when changing it's name before saving it - I end up with having same reference.

Due to it, I can't control copying values to runtime from a pickled object (partial pickled..)

What can I do?

Example:

class A:
  some_attribute = ""

A.some_attribute = "a new runtime data"

with open("pickled_object.pk", "wb") as pk:
     pickle.dump(A, pk)

----
"" different startup ability ""

with open("pickled_object.pk", "rb") as pk:
     A = pickle.load(pk)

I hope you got the idea, I will edit in case needed more information..

ForMartha
  • 107
  • 1
  • 10
  • 2
    What you’re describing is *the whole point* of a singleton. Only a single object instance of it should exist, and it shouldn’t be copyable. Given that, it’s unclear what behaviour you’re expecting. – Konrad Rudolph Aug 26 '20 at 10:29
  • "The problem is that the object type/name is identical to the in memory object, and due to it, even when changing it's name before saving it - I end up with having same reference." I'm not understanding this – juanpa.arrivillaga Aug 26 '20 at 10:43

0 Answers0