Python4Kids: New Tutorial – An Awful Pickle

[ Thanks to Brendan Scott for this
link. ]

“So, we know how to save trivia questions to a file,
and how to read them back from a file in the future. Moreover, we
have decided on a particular way of structuring the data which
makes a question. That is, the question is followed by the correct
answer and then a number of incorrect answers. Now we have to
translate between a list (which has a concept of elements), and a
file (which doesn’t). Files are “flat” – which is to say that
they have no sense of structure, they are simply a stream of data.
A file may record all of the characters which are the questions and
answers, but it wouldn’t record the fact that they are a list or,
indeed, that they are any kind of Python object. I was originally
just going to run with this to let you find out about files, but I
have instead decided to introduce a further concept – the
Python pickle!

“pickle is a module which allows you to store Python objects
including their structure. That means after you have pickled an
object to a file, you can later load that object back up from the
file and all the structure associated with that object will be


Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends, & analysis