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

  Рет қаралды 86,128

Indently

Indently

Күн бұрын

Пікірлер: 62
@evlezzz
@evlezzz Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@TheGoldenPro tbh if you have write access you could also just overwrite the program with malicious code
@md.redwanhossain8822
@md.redwanhossain8822 Жыл бұрын
@@TheGoldenPro just use hashing
@dzendys_
@dzendys_ Жыл бұрын
@@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 Жыл бұрын
Cool video, thanks for the disclaimer at the end, these types of videos rarely mention the downsides of certain methods
@JustComputers-qn2et
@JustComputers-qn2et 8 ай бұрын
" 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?
@LukeMartinVideo
@LukeMartinVideo Жыл бұрын
Lovely quick and easy lesson. Love it. Very instructive too !
@catloafl
@catloafl Жыл бұрын
This is pretty awesome; I will definitely use this in my future projects! Thanks for the tutorial 😄😄
@jaimefernandopelembe4339
@jaimefernandopelembe4339 14 күн бұрын
Your video help me very much, thank you dude
@barelycodingtoday
@barelycodingtoday Жыл бұрын
I used this today on a Project. Thanks!!
@oshriperetz2538
@oshriperetz2538 Жыл бұрын
For shorter, more concise and safer code, look into dataclasses and the "dacite" package :)
@Indently
@Indently Жыл бұрын
I'll check it out! Thanks for sharing!
@marksegall9766
@marksegall9766 8 ай бұрын
'data.bin' is a typical file name for the pickle module. Pickle creates binary files.
@VictoriaMichelleBeltran
@VictoriaMichelleBeltran Ай бұрын
Are we suppose to import pickle before our coding?
@anoops7974
@anoops7974 Жыл бұрын
Thank you so much. Your teaching is very concise and I was able to understand it super easily.
@NewsSphereNetwork
@NewsSphereNetwork Жыл бұрын
This is a good one, Didn't know the real usage of pickle before, Tnx
@jma42
@jma42 Жыл бұрын
pickling isnt safe so it should only be used on cases were you trust the data, like cache or something
@Indently
@Indently Жыл бұрын
I mentioned that in the video
@chikezieezenna1629
@chikezieezenna1629 4 ай бұрын
I will know the usefulness of pickling a model for example a machine learning model you have trained.
@chrisnorthall8317
@chrisnorthall8317 7 ай бұрын
doing my CS MSc using pickle is a big no no, assignment gradings are penalised for doing so. I can see the appeal...
@ashersaipe
@ashersaipe Жыл бұрын
wow this is so useful thank you! nice video as well, very clear :)
@danielniels22
@danielniels22 Жыл бұрын
thanks! really useful for me as a Python beginner. Subscribed!
@EM8844
@EM8844 2 ай бұрын
I will pickle today, thanks
@GamingAbroad90
@GamingAbroad90 Жыл бұрын
Id personally use it on a vm or public computer if it was a random pickle file
@mlocverm
@mlocverm Жыл бұрын
Very useful Info, Thank you!
@adrijeguha9806
@adrijeguha9806 Жыл бұрын
What's the difference between object creation like the one shown in the video and `fruit = Fruit('Banana', 100)`.
@dev_expo
@dev_expo Жыл бұрын
KZbin 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
@riyalmasla
@riyalmasla Жыл бұрын
Check out dill if you want to export objects as files. dill was a very useful module for me.
@69k_gold
@69k_gold 8 ай бұрын
Why can't we just put the object's .__dict__ into a JSON instead?
@Mulakulu
@Mulakulu Жыл бұрын
Could you please remove that constant bass-hum in audacity or something? This is quite unpleasant to listen to
@Indently
@Indently Жыл бұрын
Sorry about that, I noticed it a couple of videos too late, it will be removed soon!
@schlopping
@schlopping Жыл бұрын
@@Indently thank you for listening to the audio feedback!
@Indently
@Indently Жыл бұрын
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 Жыл бұрын
@@Indently understandable, it's a very easy trap to fall into lol
@J.erem.y
@J.erem.y 9 ай бұрын
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.
@qwerty_qwerty
@qwerty_qwerty Жыл бұрын
underrated ngl
@yusinwu
@yusinwu Жыл бұрын
Will this work if the python script loading the pickle file doesn't have access to the class definition?
@Indently
@Indently Жыл бұрын
From the top of my memory, no.
@yusinwu
@yusinwu Жыл бұрын
@@Indently that's unfortunate.... it seems like I will have to install sklearn on my SBC. Thanks for the content, very helpful and informative
@miguelvasquez9849
@miguelvasquez9849 Жыл бұрын
great video, like the ones you have done before
@supritobiswas
@supritobiswas Жыл бұрын
Good content, but can you look into the constant buzzing noise in the audio?
@Indently
@Indently Жыл бұрын
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.
@kreont1
@kreont1 Жыл бұрын
Can you show this "binary" file, please ?
@Indently
@Indently Жыл бұрын
No
@Ferret2YourFace
@Ferret2YourFace Жыл бұрын
@@Indently can you show it to ME? Im fairly trustworthy.
@someon3
@someon3 Жыл бұрын
Pickle Rick
@letshev
@letshev Жыл бұрын
You know, there is actually module called Flask-WTF. WTF is WTF?)
@hlubradio2318
@hlubradio2318 7 ай бұрын
Nice
@48-_
@48-_ Жыл бұрын
from food import pickle
@48-_
@48-_ Жыл бұрын
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
@RudolfKlusal
@RudolfKlusal Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 😞 )
@DavidLouisson
@DavidLouisson 8 ай бұрын
@@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_ Жыл бұрын
Thank you.
WHAT Is "Glob" In Python?! (It's Actually Very Useful!)
11:15
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 286 М.
Happy birthday to you by Secret Vlog
00:12
Secret Vlog
Рет қаралды 6 МЛН
Amazing remote control#devil  #lilith #funny #shorts
00:30
Devil Lilith
Рет қаралды 16 МЛН
Family Love #funny #sigma
00:16
CRAZY GREAPA
Рет қаралды 61 МЛН
Serialize Python Objects With Pickle
11:36
NeuralNine
Рет қаралды 32 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 610 М.
Avoid These BAD Practices in Python OOP
24:42
ArjanCodes
Рет қаралды 72 М.
Modern Graphical User Interfaces in Python
11:12
NeuralNine
Рет қаралды 1,6 МЛН
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,2 МЛН
This INCREDIBLE trick will speed up your data processes.
12:54
Rob Mulla
Рет қаралды 268 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 814 М.
Python Tutorial: Working with JSON Data using the json Module
20:34
Corey Schafer
Рет қаралды 1,1 МЛН
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
The Fastest Way to Loop in Python - An Unfortunate Truth
8:06
mCoding
Рет қаралды 1,4 МЛН