1
instance1 = class_A()
shelve["key1"] = instance1

So instance1 is a reference to an instance of class_A stored in memory.

Does the above code dereferences the reference instance1 and stores the underlying object instance in shelve? Or does it only store the reference?

Because I don't want to store just the reference instance1 and when the program closes, the underlying object gets released, then the instance1 reference becomes invalid on next program start.

martineau
  • 119,623
  • 25
  • 170
  • 301
NoName
  • 9,824
  • 5
  • 32
  • 52
  • Shelves are like persistent dictionaries and the values in this case would be instances of `class_A`. When the program restarts and reopens the shelf later, the keys and associated values (the instances of `class_A`) will all still be there. – martineau Oct 07 '19 at 00:44

1 Answers1

1

Shelves don't store references to in-memory instances. They serialize objects with pickle and store serialized representations, from which objects can later be recreated. It would be impossible for shelve to do its job if it tried to store references (and trying to store "the underlying object instance" runs into similar problems).

user2357112
  • 260,549
  • 28
  • 431
  • 505