WHAT Is "Pickle" In Python?! (EXTREMELY Useful!)

Sdílet
Vložit
  • čas přidán 5. 09. 2024
  • What is "Pickle" in Python? Well, pickle is a module that allows us to serialise and deserialise objects in Python. So that we can save some more complex/ custom data types such as instances from our classes!
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels

Komentáře • 60

  • @evlezzz
    @evlezzz Před rokem +168

    You went to a very dangerous territory here...
    Using pickle to store state of objects in long term has a lot more caveats than just "don't load random stuff from the internet'.
    1. Pickle is Python only format. You can't read it from anything else or look at stored data. Even within python there are several versions of pickle and you can't load files stored in newest pickle format on older versions of python. (Last change happened at python 3.8)
    2. More importantly, pickle depends deeply on the structure of your code, modules and classes. You can only safely unpickle objects that were stored with the same code structure. Programs tends to change over time. Storage formats change less often and it's easier to consider what to do with old data. Add new attribute to your class or rename one. If you attempt to load pickle file stored in previous version, an object will be restored with attributes as they were in a previous version and they probably won't match the changes you made. Rename the class or put it into different module - and loading will fail.
    3. There is a reason why saving into json takes a lot of repeating code: you actually need to say explicitly, what are you planning to store and what name should be attached to it. Your classes could contain some data that is deduced on the fly and there is no use in storing and loading them. And without additional code pickle would just store everything from object (well, technically not everything, there are some exceptions by default). You could specify what to pickle and what not to pickle, but then we return to the same state as before which is - we need to be explicit about what to store and how.
    4. There are proper ways to stay with normal interchangeable formats and not writing too much boilerplate. Projects like pydantic, dataclass-json, dataclass_wizard, dataclass_factory and others allows you to specify mapping between most of formats (like json, yaml, or some dictionary loaded from sql database) and your classes.
    5. Main purpose of pickle remains to be short term storage of objects so they could be transferred between parts of the system that you maintain, for example, for interprocess communication or sending tasks to worker farm run by celery. And pickle was never intended for long term storage of the system state like you are implying.
    To summarise: don't use pickle this way if that's not a one-time task and you are planning to use what you have written two month later, there are better solutions.

    • @Indently
      @Indently  Před rokem +29

      I'm sure that would make for a much more interesting video than what I explained in 10 minutes. Thanks for the informative comment! I will look into creating a video that goes far more in depth.

    • @TheGoldenPro
      @TheGoldenPro Před rokem

      I also see a potential vulnerability, wouldn't you be able to just rewrite this pickle file from another python application and embed some malicious code. Even more of a reason to stay away from it for any long term storage.

    • @Susul-lj2wm
      @Susul-lj2wm Před rokem +6

      @@TheGoldenPro tbh if you have write access you could also just overwrite the program with malicious code

    • @md.redwanhossain8822
      @md.redwanhossain8822 Před rokem

      @@TheGoldenPro just use hashing

    • @dzendys_
      @dzendys_ Před rokem

      @@md.redwanhossain8822 i think the main purpose of pickle is for easy and small project. People with massive code will eventuelly use json or other formats.

  • @ddesy
    @ddesy Před rokem +2

    Cool video, thanks for the disclaimer at the end, these types of videos rarely mention the downsides of certain methods

  • @JustComputers-qn2et
    @JustComputers-qn2et Před 6 měsíci +2

    " with open('banana.json', 'w') as file:
    json.dump(fruit.__dict__, file) "
    &
    " with open('banana.json', 'r', encoding='utf-8') as file:
    fruit = Fruit(**(json.load(file)))"
    Does this not achieve the same thing but for json? rather than a need to manually create the data you could dump the object as a dictionary?

  • @catloafl
    @catloafl Před rokem +3

    This is pretty awesome; I will definitely use this in my future projects! Thanks for the tutorial 😄😄

  • @LukeMartinVideo
    @LukeMartinVideo Před rokem +2

    Lovely quick and easy lesson. Love it. Very instructive too !

  • @chikezieezenna1629
    @chikezieezenna1629 Před měsícem

    I will know the usefulness of pickling a model for example a machine learning model you have trained.

  • @oshriperetz2538
    @oshriperetz2538 Před rokem +3

    For shorter, more concise and safer code, look into dataclasses and the "dacite" package :)

    • @Indently
      @Indently  Před rokem

      I'll check it out! Thanks for sharing!

  • @anoops7974
    @anoops7974 Před 11 měsíci

    Thank you so much. Your teaching is very concise and I was able to understand it super easily.

  • @barelycodingtoday
    @barelycodingtoday Před rokem

    I used this today on a Project. Thanks!!

  • @EM8844
    @EM8844 Před 11 dny

    I will pickle today, thanks

  • @marksegall9766
    @marksegall9766 Před 6 měsíci

    'data.bin' is a typical file name for the pickle module. Pickle creates binary files.

  • @jma42
    @jma42 Před rokem +2

    pickling isnt safe so it should only be used on cases were you trust the data, like cache or something

    • @Indently
      @Indently  Před rokem +1

      I mentioned that in the video

  • @NewsSphereNetwork
    @NewsSphereNetwork Před rokem

    This is a good one, Didn't know the real usage of pickle before, Tnx

  • @ashersaipe
    @ashersaipe Před rokem +1

    wow this is so useful thank you! nice video as well, very clear :)

  • @danielniels22
    @danielniels22 Před rokem

    thanks! really useful for me as a Python beginner. Subscribed!

  • @riyalmasla
    @riyalmasla Před rokem

    Check out dill if you want to export objects as files.
    dill was a very useful module for me.

  • @adrijeguha9806
    @adrijeguha9806 Před rokem

    What's the difference between object creation like the one shown in the video and `fruit = Fruit('Banana', 100)`.

  • @mlocverm
    @mlocverm Před rokem +1

    Very useful Info, Thank you!

  • @chrisnorthall8317
    @chrisnorthall8317 Před 5 měsíci

    doing my CS MSc using pickle is a big no no, assignment gradings are penalised for doing so. I can see the appeal...

  • @qwerty_qwerty
    @qwerty_qwerty Před rokem +1

    underrated ngl

  • @GamingAbroad90
    @GamingAbroad90 Před rokem +1

    Id personally use it on a vm or public computer if it was a random pickle file

  • @miguelvasquez9849
    @miguelvasquez9849 Před rokem

    great video, like the ones you have done before

  • @dev_expo
    @dev_expo Před rokem

    CZcams suggested the video at very right time.
    I was trying to save a pandas dataframe in database and I think this will do the job.
    Whole dataframe can be saved in single cell in a pickle I guess

  • @Mulakulu
    @Mulakulu Před rokem +33

    Could you please remove that constant bass-hum in audacity or something? This is quite unpleasant to listen to

    • @Indently
      @Indently  Před rokem +22

      Sorry about that, I noticed it a couple of videos too late, it will be removed soon!

    • @schlopping
      @schlopping Před rokem +10

      @@Indently thank you for listening to the audio feedback!

    • @Indently
      @Indently  Před rokem +13

      I now always have my EarPods in for monitoring to avoid unpleasant surprises like this. I was editing the video without headphones so I didn't even notice that humming until I put my headphones on a couple of days later 🥲

    • @r0zemary
      @r0zemary Před rokem +1

      @@Indently understandable, it's a very easy trap to fall into lol

    • @J.erem.y
      @J.erem.y Před 7 měsíci

      Sounds like a good project for you Mulakula, an active low pass filter so you don't have to ask content creators to change how they record videos.

  • @69k_gold
    @69k_gold Před 6 měsíci

    Why can't we just put the object's .__dict__ into a JSON instead?

  • @someon3
    @someon3 Před rokem +3

    Pickle Rick

  • @hlubradio2318
    @hlubradio2318 Před 4 měsíci

    Nice

  • @supritobiswas
    @supritobiswas Před rokem +1

    Good content, but can you look into the constant buzzing noise in the audio?

    • @Indently
      @Indently  Před rokem

      Yeah unfortunately that happened for 2 videos, I learned it was because my iPhone was connected to my adapter and for some reason that created this noise, it will be gone in a couple videos.

  • @yusinwu
    @yusinwu Před rokem

    Will this work if the python script loading the pickle file doesn't have access to the class definition?

    • @Indently
      @Indently  Před rokem

      From the top of my memory, no.

    • @yusinwu
      @yusinwu Před rokem

      @@Indently that's unfortunate.... it seems like I will have to install sklearn on my SBC. Thanks for the content, very helpful and informative

  • @kreont1
    @kreont1 Před rokem

    Can you show this "binary" file, please ?

  • @48-_
    @48-_ Před rokem

    from food import pickle

    • @48-_
      @48-_ Před rokem

      from food.buns import *
      from food import fried_patty
      from food import salad
      from food.sauces import mayo,mustard,ketchup
      from food import onion,tomatoes
      friedpattyc=food.fried_patty.config(beef=True,ground=True)
      # NOTE: its a joke

  • @letshev
    @letshev Před rokem

    You know, there is actually module called Flask-WTF. WTF is WTF?)

  • @RudolfKlusal
    @RudolfKlusal Před rokem

    Why the hell it's called "piclkle?" Why the hell web server is called "flask?" Why the hell xml library is called "beautifulsoup?"
    Why the hell people should taky Python seriously, if python's naming conventions are so infantile?
    I love python, but this is just what I don't get.

    • @Indently
      @Indently  Před rokem +2

      Personally, I think you should sway away from that mentality, I would relate Python to being a social language that everyone can understand. If you want to really get serious, then I recommend just using C or a lower level language.
      The comedy that comes with Python is what makes Python, Python 😉

    • @RudolfKlusal
      @RudolfKlusal Před rokem

      @@Indently I know, I am using multiple languages, I just don't get this "forced cringe." Fortunately this awesome language pushed old baddies like pascal out of Universities (yes, in 2013 as I was here, they learned Pascal as a main language 😞 )

    • @user-vp3vs6ey3r
      @user-vp3vs6ey3r Před 5 měsíci

      @@IndentlyIndeed, even Python is arguably an odd name for a computer language. Who would name their premier software masterpiece after a TV comedy? Guido apparently has an offbeat sense of humor, and surely his followers could be excused for continuing in a similar vein.

  • @Dubai_life_
    @Dubai_life_ Před rokem

    Thank you.