[DB-SIG] Question on shelve/pickle and namespaces...

Chris Cogdon chris at cogdon.org
Thu Feb 5 00:00:36 EST 2004

On Feb 4, 2004, at 20:41, Stuart Hungerford wrote:

> Hi all,
> I hope this is a suitable forum to ask this shelve related
> question: I figured if any group or SIG would know, it would
> be this one.
> I have a python module foo.py which contains:
>    class A(object): ...
>    class B(object): ...
> As well as a class that uses shelve (which in turn is
> using dumbdbm):
>    class foo(object): ...
>       // uses a shelve internally
> The methods of foo shelve and restore A and B instances
> *but* shelve seems to be looking in the wrong places for
> the definitions of A and B when they're "unshelved",
> giving the error:
>    AttributeError: 'module' object has no attribute 'A'
> How can I persuade shelve to look in other namespaces for
> A and B?
> Any advice much appreciated,

Simply, make sure that the function or method that unshelves objects 
has the same namespace setup (with regards to classes and modules) as 
the setup that shelves them. The easiest way to do this is to have the 
shelving and unshelving in the same location, or nearly same location, 
in the code, such as putting all the shelving and unshelving into 
methods of the same class.

    ("`-/")_.-'"``-._        Chris Cogdon <chris at cogdon.org>
     . . `; -._    )-;-,_`)
    (v_,)'  _  )`-.\  ``-'
   _.- _..-_/ / ((.'
((,.-'   ((,/   fL

More information about the DB-SIG mailing list