Array of objects lost in unpickling

B

Bahadir

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
 
J

Jon Clements

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.
 
B

Bahadir

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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top