You are viewing a single comment's thread from:

RE: Learn Python Series (#28) - Using Pickle and Shelve

in #utopian-io6 years ago

Woah, I loved how expository your code was. I am also a python programmer but am not too familiar with this module. Thanks for the tutorial. A question though. Why is the dump function there when a suitable alternative would be to commit the dictionary to a file simply using such code for example

       x = open(filename, 'w+')
        x. write(fav_cryptos)
        x. close 

Wouldn't this append the file to the document or is it for a more peculiar purpose?

Sort:  

Hi, and thx! ;-)

I've discussed 2 modules, pickle and shelve (they're two different modules, where shelve is built on top of pickle). The key take-away of this episode is to explain that dumping via pickling is a way to "serialize" a Python object (for example a dictionary or a list), which is kept in RAM and only works "inside the program), and is then "pickled" to save in a file in a binary mode, where the pickle contains the full instructions to un-pickle, ergo, to read it back, by another or the same program at another time, after the program was closed, and to put it back in RAM.

Simple use case? When you're playing a game and you're saving your progress on level 14 before you to to sleep. Saving the "game state" == pickling ;-)

Ooh wow
That's elucidiated things
I fully get the application now
Its like saving a data state and all the changes with a single module. That's awesome
Thanks

Hey @yalzeee
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

Contributing on Utopian
Learn how to contribute on our website.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 57651.10
ETH 2377.43
USDT 1.00
SBD 2.42