On 07/06/2019 18:42, Gursimran Maken wrote: > I am not getting the concept of pickle and shelves in python, I mean what's > the use of both the concepts, when to use them in code instead of using > file read and write operations.
You are right in that you could save all your data to a file using read/write primitives. Of course you'd need to convert them all to/from strings. And all collections would need to be iterated over (and if the collection contained other collections they'd need to be iterated too). But with pickle you can just save the entire data structure with all its contents, in a single operation and it can be a collection (of any type and depth) and the contents can be any kind of mixed data. You don't need to care, pickle takes care of it all for you. And you can read it back again in a single operation and all the data will be restored to its original type and location. But with pickle you still need to store things in sequence and read them back out in the sequence that you stored them. If all you are doing is saving the state of your program and restoring it that isn't a problem. But if you only want to retrieve one piece of your data later then its a bit of a nuisance. That's where shelve comes in. By acting like a persistent dictionary you can assign keys to your data and then restore it in any sequence, or only restore some of it, you can. Does that help? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor