InfinityCodeX

Unlock the power of Python, Data Science, Machine Learning, and Deep Learning with our comprehensive guides! Whether you're a beginner eager to dive into AI or an experienced professional looking to sharpen your skills, our blog offers easy-to-follow tutorials, insightful tips, and expert advice to fuel your AI career. Dive in today and start mastering the cutting-edge technologies shaping the future!

Pickle Python








So everyone of you must be aware about pickle which we eat in our day to day life. Lets understand that pickle before diving into the python’s pickle. So where do we store pickle? We store it in an air tight container & when we want to eat it we open the container remove the pickle & close the air tight container back.

Now let us understand python’s pickle basically it converts python object to byte stream & vise versa. Now what that exactly means?...If you come with Java background you would be aware of serialization. For those who does not belong to java background serialization in java is known as pickle in python.

Let’s simplify pickling it is use store python objects this could be object you had create from a class, list, dictionary etc. Don’t worry I’ll show you example of pickling list & remove it from it from container when ever we want it. Now you must be thinking why there is concept of pickling? Let’s relate it to real pickle… why do store pickle it in a container the answer is pretty simple doesn’t it…Because it is time consuming to make it & it can’t be prepare in an instant. So now relate that thing to the programming. Think that you had created an class or a list that you want to use again & again instead of calling that class again & again import that pickle object & you are good to go.

Now let’s take an example to create a pickle list.

*Generating & Extracting a pickle file :

file_1 : p_file1.py

#Generating a Pickle file

import pickle
subjects = [‘Maths’, ‘Science’, ‘English’]
f_name = “Mypickle.pkl”           #.pkl is not mandatory
f_obj = open(f_name, ‘wb’)        #write in bytes
pickle.dump(subjects, f_obj)
f_obj.close()

file_2 : p_file2.py

#Extracting a Pickle file

import pickle
f_name = “Mypickle.pkl”
f_obj = open(f_name, ‘rb’)            #read in bytes
sub = pickle.load(f_obj)                #load the file object
print(sub)                                  #print all the files which we had pickled


Note : Now if you will change the values of the subjects it will directly effect to the pickle you have called in the p_file2.py.

So the whole point of using pickle is that some time you have to use few line of codes in multiple scenarios instead of copy pasting just create a pickle file of it & use it anywhere you want. Let’s take an example there is a dictionary of students marks & you had create an pickle of that dictionary & used in multiple location now you forgot where you have used it. So instead of finding that dictionary to all the files of code just go to your pickle file change the values & save it & re run the program again it will update that value at all the places automatically.

Hey guys, did you liked our content feel free to comment below & don't forget to share it with your friends & family.

No comments:

No Spamming and No Offensive Language

Powered by Blogger.