0

I'm using Python 3.9 and I try to store an object with pickle. Although, this object contains an object list in which is contained another object list.

If I make the first object, store it using pickle, then delete it and retrieve it, without stoping the program, I have the list of the other object.

If I stop the program and then try to retrieve it, the list of the other object seems to be empty.

The class I've made looks like this:

class First():
   list_of_Second = []
   info = 0

   def __init__(self,...,):
      ...


class Second():
   list_of_Third = []
   info = 0
   ...


class Third():
   info = 0
   ...


def get_First_class_from_file(filename):
    with open(filename, 'rb') as f:
       return pickle.load(f)

So, when I try to retrieve my class from file, I get an empty list_of_Second. All the other info in the first class is retrieved.

Edit, partly solved thanks to @juanpa.arrivillaga: I was using class-variables so it didn't work. If I put the list in the constructor, I will use an instance-variable and it will work for storing the list_of_Second. Although, the list_of_Third is empty again. The code looks like this:

class First():
   info = 0

   def __init__(self,...,):
      list_of_Second = []
      ...


class Second():
   info = 0

   def __init__(self,...,):
      list_of_Second = []
      ...


class Third():
   info = 0
   ...


def get_First_class_from_file(filename):
    with open(filename, 'rb') as f:
       return pickle.load(f)
5
  • Because you are using class-variables. Commented Dec 23, 2020 at 9:49
  • Thanks, for the response. So it works with instance-vars but I have another problem. I want to store another list of another object in this file. I'm using instance-var for my list but it doesn't work again. Commented Dec 23, 2020 at 10:07
  • You really must provide a minimal reproducible example. Please see How to Ask and check out the help center Commented Dec 23, 2020 at 10:08
  • Alright I updated my problem. Commented Dec 23, 2020 at 10:20
  • 1
    That is not minimal reproducible example. Again, please see How to Ask and look at the help center Commented Dec 23, 2020 at 10:24

1 Answer 1

0

I had not the whole code so here is the complete explanation:

Pickle dump and load instance only, it is explained in the documentation: https://docs.python.org/3/library/pickle.html

similarly, classes are pickled by named reference, so the same restrictions in the unpickling environment apply. Note that none of the class’s code or data is pickled, so in the following example the class attribute attr is not restored in the unpickling environment:

class Foo:
    attr = 'A class attribute'

picklestring = pickle.dumps(Foo)

These restrictions are why picklable functions and classes must be defined in the top level of a module.

But classes ARE instance of their metaclass (usually object).

list_of_Second is a class attribute, not an instance one. You must serialize your class as shown above to be able to retrieve class attributes.

You will need to load the class before loading its instances.

Sign up to request clarification or add additional context in comments.

6 Comments

So, you're saying that it's not possible to serialize a class with a class list, right? Do you know any way to do it?
@n45os No, what i am saying is that you are trying to serialize CLASS attributes and not INSTANCE attributes. If you want to serialize the classe attribute, you must serialize the classe, then load its instances. If you only try to serialize the instances, you will never get the classes attributes. As shown in the documentation, do: pickle.dump(First, myfile) to serialize the class
But if I try to serialize the class I won't get all the information I have given to my instances.
Please, read the answer given correctly. 1. deserialize the CLASS 2. deserialize its INSTANCE
I'm not sure the response understood the OP's question fully. I interpret as trying to serialize instance attributes, not class attributes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.