Home > Net >  How can I set the Type of an Unpickled Object?
How can I set the Type of an Unpickled Object?

Time:12-11

I am unpickling an object (chocolate) that belongs to the class Food via:

chocolate = pickle.loads(chocolate_pickled)

Assuming I have a Food import at the top of my file, how can I tell python that chocolate belongs to the Food class?

CodePudding user response:

how can I tell python that chocolate belongs to the Food class?

You don't. the pickle file contains the data about the type of object it has pickled. It thus does not only 'serialize" the "data", but also the "meta-data".

You should however be careful to pickle and unpickle relatively to the same type, so better in the same Python file.

  • Related