Array of objects lost in unpickling

Bahadir bilgehan.balban at gmail.com
Sun Sep 13 17:28:02 CEST 2009


On Sep 13, 5:48 pm, Jon Clements <jon... at googlemail.com> wrote:
> On 13 Sep, 15:19, Bahadir <bilgehan.bal... at gmail.com> wrote:
>
>
>
> > Hi,
>
> > I have a class:
>
> > class second:
> >     a = None
> >     b = None
>
> > class first:
> >     array = []
>
> > I populate the array in first class with instances of second, then
> > save by:
>
> > shelve = shelve.open(),
> > shelve["first"] = myfirst
> > shelve.close()
>
> > When I reopen the shelve from another script, the first class is
> > there, but array has no elements. If I reopen the shelve in the same
> > script right after shelve.close(), the elements are there.
>
> > Also there are no errors printed out.
>
> > Any idea why the array of instances are lost?
>
> > Thanks,
>
> > Bahadir
>
> You most likely want 'array' to be an instance level and not class
> level attribute.
>
> class first(object):
>     def __init__(self):
>         self.array = []
>
> myfirst = first()
> myfirst.array.append(23423)
>
> etc...
>
> hth,
> Jon.

Hmm, OK. New to python. Got it. Thanks a lot!

Bahadir



More information about the Python-list mailing list