How can I set the Type of an Unpickled Object?

Question:

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?

Asked By: Conor Romano

||

Answers:

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.

Answered By: Willem Van Onsem
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.