shelve non-transparency

Paul Rubin http
Wed Aug 24 01:57:16 EDT 2005


    class x: pass
    z = x()
    z.a = 'a'

    d = {'a': z}
    for i in range(5):
      print id(d['a'])

prints the same id 5 times as you'd expect.

    d = shelve('filename')
    d['a'] = z
    for i in range(5):
      print id(d['a'])

prints five different id's.  So, for example,
   y = d['a']
   y.x = 'x'
fails to update the shelve.

The reason is sort of understandable but I hadn't guessed it ahead of
time (I'd actually forgotten that d wasn't a normal dict, which you're
supposed to be able to do if abstraction works properly), and it
caused a bug in my program that took a little while to figure out.

I wonder if some fix is possible.



More information about the Python-list mailing list