Data Classes in Python Are The New Standard

  Рет қаралды 67,685

NeuralNine

NeuralNine

Күн бұрын

In this video we cover dataclasses. The modern best-practice way to implement data-focused classes.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: www.neuralnine...
💻 The Algorithm Bible Book: www.neuralnine...
👕 Programming Merch: www.neuralnine...
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine...
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/Neu...
🎙 Discord: / discord
🎵 Outro Music From: www.bensound.com/

Пікірлер: 88
@BrainInteractive
@BrainInteractive Жыл бұрын
Started re-learning python for the first time in 7 years, this video and your channel have been so helpful!
@HelmutQ
@HelmutQ 10 ай бұрын
Vermutlich hat mir KZbin ihren Kanal vorgeschlagen, weil ich auch Österreicher bin, Das war für mich ein Glücksgriff. Unprätenziöse, technisch hervorragende Information ohne Schnickschnack. Genau sowas brauche ich bei meinen verschiedenen Hobbies. Chapeau. Man sieht da ist ein absoluter Überflieger am Werk, der es nicht nötig hat, besonders unterhaltend und originell zu sein. Viel Erfolg. Dass Sie Österreicher sind habe ich erst bemerkt als Sie es gesagt haben.
@MrGeordiejon
@MrGeordiejon 13 күн бұрын
Yeah.., Helmet
@thomasgoodwin2648
@thomasgoodwin2648 3 жыл бұрын
Very 😎. I didn't even know this existed. My biggest complaint of programming in any language is the amount of boilerplate code needed to get anything more than "hello world" done. Repr/str/compare, etc in python, etc, getters n setters in the C farm of languages etc, etc. Etc. Not to mention the endless documentation, type hinting, etc etc etc etc (in etc sauce). Seems that this type of endless bs 'gotta be done' programming is where code completion models could really shine. You (the programmer) concentrate on the algorithm (state function, whatever), and the rest of the thing writes itself up to and through unit testing. Thanks tons! This is definitely going to help in quite a few places.
@thomasgoodwin2648
@thomasgoodwin2648 2 жыл бұрын
@@BobAg_ When putting up a shelf you could cut down a tree, trim the log, cut it, plane it, cure it, etc, however most peeps these days would buy a set of precut boards to suit their needs. I can't say I disagree with you about speed issues going through multiple layers of abstraction. Back in 'the good old days' we programmed right down to the metal in assembly. These days program requests are passed along to the O.S. supervisor for consideration, we'll get back to you when it's done. Clearly going to slow things down. In this case though I'm not really talking about performance issues, but rather programmer load, and how compilers and IDEs can help ease the keyboard workload. Say I'm in Python, and I'm creating um.. a bunch of game objects. Every time I create a new object I have to write REPR functions for each object type, which are for most intents and purposes identical. It would be nice if the IDE could at least automatically place a __repr__ stub including all variables defined in __init__. If I don't have to type that junk in, I can concentrate more getting through the hairier code instead of mindlessly tapping keys or even copy pasting. It may not seem like much case by case, but typing but all this junk in does take (cumulative and irreplaceable) time. Not so bad I guess if you're a salary enterprise programmer, who cares how long it takes as long as you get paid. It really tends to slow you down however if your trying to prototype a controller for your new laser potato masher (or whatever). Excess boilerplate code tends to lead to unnecessary errors and wasted time. Some languages are better than others in this regard. I just feel that this is in general a neglected aspect of language design.
@fcode9310
@fcode9310 3 жыл бұрын
Great video! I just was wondering what palette of colors are you using for your code in pycharm? It looks cool!
@olivomiguel
@olivomiguel 3 жыл бұрын
Atom One Dark
@ethanrandall3005
@ethanrandall3005 Жыл бұрын
​@@olivomiguel Wait, does pycharm have a light mode? It's always looked like this for me (as long as I can remember).
@Sinke_100
@Sinke_100 3 жыл бұрын
I am curious about that equal, perhaps when default sort function is the same, maybe it uses first value so john < mike if they have same cash?
@2ru2pacFan
@2ru2pacFan 3 жыл бұрын
Thanks dude, makes life so much easier!
@davidnaneishvili
@davidnaneishvili 3 жыл бұрын
Making simple text rpg game seems like a great learning of classes
@wuda-io
@wuda-io 3 жыл бұрын
It is so crazy, after watching python after working a while with JS and PHP, where are all the brackets?? 😂
@dohnot
@dohnot 3 жыл бұрын
Thanks for this video. It will make my work much more easier!
@MrGeordiejon
@MrGeordiejon 13 күн бұрын
Good video 🔔👍 I don't see the advantage of the sorting stuff? Added boiler plate, more verbose, decorator args and post_init! how is it better than using code comprehension? my_list.sort(key=lambda investor: investor.cash) or sorted(my_list, key=lambda investor: investor.cash, reverse=True) And how come sort_index got added at the top with defaults? Is this a special case because init = False?
@dineshr93
@dineshr93 3 жыл бұрын
is sort_index is inbuilt variable for controlling the sorting ? Thanks for the great video.
@aflous
@aflous Жыл бұрын
noo, you can call foo or bar or whatever, there is no magic there. The only condition is to put it at the 1st position compared to the remaining attrs as dataclasses uses all the attrs in the defined order for comparison
@AminAlan
@AminAlan 10 ай бұрын
the data classes are very useful, and scalable, but you can put documentation to each attribute, such as the property docstring?
@learntocode5464
@learntocode5464 3 жыл бұрын
Excellent and high level toturial, keep up, thanks from Morocco
@codeloki
@codeloki Жыл бұрын
Hey Nacho, great to see you leave cartel and started your own youtube..
@SAlexandert8
@SAlexandert8 3 жыл бұрын
Indeed awesome, I didn’t find many gotchas to not use them. Will test them out, thanks!
@funkmedaddy
@funkmedaddy 3 жыл бұрын
the only downside i found atm is that whenever u have a default value set for a field, then all the fields that come after that one have to also be default, which includes subclasses, so if u inherit from a class with field that has a default value then that class can only have default value fields
@messilionel2827
@messilionel2827 3 жыл бұрын
What's the intro music?
@hell0kitje
@hell0kitje 3 жыл бұрын
hey im trying to get into secutiy job, one of the resposibles it is to write phyton fuzzers, can you make simple video about it?
@hectorminator4
@hectorminator4 3 жыл бұрын
I don't quite get how do you sort by cash, because, you define that the sort_index is equal to the cash, is this sort_index an special variable or something that the comparison method in dataclasses uses to actually compare the instances of the classes??
@tapsfest23
@tapsfest23 3 жыл бұрын
If I have to guess I think it takes the first value of the data class by default as he is defining it at the top.
@jrstf
@jrstf 5 ай бұрын
@@tapsfest23 - I too can guess that if sort_index has equality it moves on to comparing the other fields. That's why comparison for equality doesn't work if you're expecting it to compare only sort_index.
Жыл бұрын
15:05 Why do you need an extra field here? Can't you just use the existing `cash` field? Looks to me like that extra property only makes things worse since it has to be manually updated of the `cash` prop is updated.
@lutfullo-n9z
@lutfullo-n9z Жыл бұрын
i have learned a great deal of information about data classes , thanks
@rishitpant
@rishitpant 3 жыл бұрын
This was helpful. Thanks.
@HiltownJoe
@HiltownJoe 2 жыл бұрын
At about 4:20 you are implementing __repr__() to modify the print output, I have seen it elsewhere too. I on the other hand had learned that you should use __repr__() in a way that calling eval() on the output will give you the objet back and you should implement __str__() if you want a nice presentation that is not for the machine. Am I missing something here, and there is a Good reason, or is it just "lazyness", because best practices are not allways best for you.
@lilDaveist
@lilDaveist Жыл бұрын
As bad as it sounds but NeuralNine often times makes mistakes that aren’t just mistakes that you do by accident. There is a big chance that he just doesn’t know. Look at his way of handling .endswith in his fuzzywuzzy video. He uses a generatorexpression inside a tuple() to have multiple file endings handled. Even though endswith already works with multiple.. he is just not a really good programmer. Edit: And he does this to do it: if file.endswith(tuple(c for c in list_of_endings) What he essentially did is cast a tuple from a list with a generator expression for every single file inside his directories.
@JohnMacPherson-hr4yz
@JohnMacPherson-hr4yz Жыл бұрын
@@lilDaveist fuzzywuzzy video? which one, could you please specify?
@lilDaveist
@lilDaveist Жыл бұрын
@@JohnMacPherson-hr4yz this one: kzbin.info/www/bejne/jX-pc3-fa6iUiq8
@hassanhilaly8810
@hassanhilaly8810 2 жыл бұрын
Very instructive , i appreciate a lot.Thank you for shiring.
@Neuroszima
@Neuroszima 3 жыл бұрын
hmmm, does it check for proper data types being entered into constructor, throwing Exceptions if not? Also can one define 'cross-class comparisons' with dataclasses? for example when i have the value of items from 2 different classes. I have made pretty custom-made class for a game and idk if dataclasses would be any usefull here, but sounds interesting Also - values set at 'post_init' work like @property decorated methods?
@quangmango7776
@quangmango7776 2 жыл бұрын
Let's say I want to write a script to retrieve Lan network IPs. In that case using Data class doesn't make sense. Just write simple functions or use normal class with __init__(). Found that concept of Data class confusing!!
@Davidkiania
@Davidkiania Жыл бұрын
This is amazing very well explained.
@rasol6460
@rasol6460 3 жыл бұрын
An easier approach to data classes, thanks for video. Keep it up!
@tridibbiswas3361
@tridibbiswas3361 3 жыл бұрын
Thank you for the video. I am dipping my toes in python. Could you share a video on how to set up vim in windows for python program
@robinpipslayertekprofitsfa2644
@robinpipslayertekprofitsfa2644 Жыл бұрын
Today is a great #MileStone for me that some might laugh at. But....I've been able to follow this WHOLE tutorial.... in a Jupyter Lab and EVERYTHING WORKS!! 🤯🤯🤯🤯🥵 🤝👏👏👏👏👏 #SimplyBrilliant and Thank You, Your videos are in my personal top 5 channels!!
@shinstorm3473
@shinstorm3473 9 ай бұрын
Nice 👍👍👍
@AliKhandi
@AliKhandi 3 жыл бұрын
what is use case of this standard?
@chriskeo392
@chriskeo392 2 жыл бұрын
use instead of standard classes
@chriskeo392
@chriskeo392 2 жыл бұрын
Can we use classmethods and staticmethods in dataclasses?
@Khmeriscool
@Khmeriscool 2 жыл бұрын
Thank you very much, this is very useful!
@anonlegion9300
@anonlegion9300 3 жыл бұрын
10:30 camelCase for JS but snake_case for Py please (if its not obvious Im just teasing, this guy is a great programmer)
@FRANKWHITE1996
@FRANKWHITE1996 3 жыл бұрын
Another great video! Thanks for sharing!!
@valenciawalker6498
@valenciawalker6498 Жыл бұрын
Thank you , excellent detailed explanation.
@radicalisdead8036
@radicalisdead8036 2 жыл бұрын
This video is incredible.
@ledempire9066
@ledempire9066 2 жыл бұрын
As usually great explanation, Thank you :)
@Snekithi
@Snekithi 2 жыл бұрын
thanks buddy. its very useful
@muthurubant5812
@muthurubant5812 3 жыл бұрын
Thank you brooo.. You are awesome 🔥
@mok0s1
@mok0s1 3 жыл бұрын
Why does the i1>i4 return ´True´ if the __gt__ method is not implemented at the beggining?
@martijnb5887
@martijnb5887 3 жыл бұрын
I gues i1>i4 = not (i4
@ethanrandall3005
@ethanrandall3005 Жыл бұрын
​@@martijnb5887 correct. Python is smart enough to do this.
@Anzeljaeg
@Anzeljaeg Жыл бұрын
Ty so much
@alex_linhares
@alex_linhares 3 жыл бұрын
Amazing; thanks!
@EugenePetrash
@EugenePetrash 2 жыл бұрын
Thanks!
@TheUser808
@TheUser808 2 жыл бұрын
How common is it to code on a windows machine?
@ethanrandall3005
@ethanrandall3005 Жыл бұрын
Almost as common as it is to be a programmer.
@pibruks
@pibruks 2 жыл бұрын
great video. thx
@globalgrowth343
@globalgrowth343 2 жыл бұрын
O waw I love your skills of explanation 👏 👌
@bayanonymous422
@bayanonymous422 3 жыл бұрын
Intro music please?
@guilherme5094
@guilherme5094 3 жыл бұрын
Nice!
@joincoder
@joincoder 3 жыл бұрын
Thank you for your sharing
@mr.terrific30
@mr.terrific30 2 жыл бұрын
luved it
@philtoa334
@philtoa334 2 жыл бұрын
Thx.
@SMURFPICTURES
@SMURFPICTURES 3 жыл бұрын
so its just a helper that gets extended and doesnt even help with the thing you mentioned at 7:15 ? wow.
@abhinavchoudhary6849
@abhinavchoudhary6849 3 жыл бұрын
I love this channel
@thatoneuser8600
@thatoneuser8600 3 жыл бұрын
Data classes are a code smell... The definition is a class with no behavior; it only contains fields, getters, and setters. The reason you should avoid these is because their data will often be manipulated by many classes in complex ways, and their lack of behavior means the class isn't doing anything to make up for having a name for its state, so its just more mental clutter to be aware of the data class in the codebase that doesn't even do anything. It also violates many OOP design principles, as the fundamental way data classes are used will lead to very low cohesion throughout the codebase.
@nagarjunsugavaneswaran
@nagarjunsugavaneswaran 3 жыл бұрын
❤️ From India
@IzUrBoiKK
@IzUrBoiKK 3 жыл бұрын
Areeee bhau!
@Lucifer-wd7gh
@Lucifer-wd7gh 3 жыл бұрын
From United States of India 😆🇺🇲🇮🇳
@IzUrBoiKK
@IzUrBoiKK 3 жыл бұрын
Lol
@alialavizadeh2775
@alialavizadeh2775 3 жыл бұрын
tnx
@Isthismylifenow
@Isthismylifenow Жыл бұрын
Sensei
@azazahmed1842
@azazahmed1842 3 жыл бұрын
broooo looove your videos but maybe elaborate a little more and explain a little bit more
@chriskeo392
@chriskeo392 2 жыл бұрын
He explained alot 😂 If you want more....go to the documentation.
@andreyshevchenko5925
@andreyshevchenko5925 3 жыл бұрын
+
@subhodeepsarkar7611
@subhodeepsarkar7611 3 жыл бұрын
First comment!
@manofqwerty
@manofqwerty 3 жыл бұрын
Why do I think this is worse?
@techizlifeog
@techizlifeog 3 жыл бұрын
firsttttttttttt
@nirmaltheprogrammer510
@nirmaltheprogrammer510 3 жыл бұрын
400 views 4 comments 😶😳
@Abhinav-Bhat
@Abhinav-Bhat 3 жыл бұрын
First
@marko90000
@marko90000 2 жыл бұрын
freakin newbs just took and type.
@ilanbar1970
@ilanbar1970 2 жыл бұрын
Nice!
Does Pydantic Replace Dataclasses in Python?
17:12
NeuralNine
Рет қаралды 32 М.
Python Itertools For Advanced Iteration
14:00
NeuralNine
Рет қаралды 10 М.
Which team will win? Team Joy or Team Gumball?! 🤔
00:29
BigSchool
Рет қаралды 14 МЛН
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 813 М.
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 308 М.
Python Data Classes Are AMAZING! Here's Why
16:11
Tech With Tim
Рет қаралды 83 М.
Python Sockets Simply Explained
39:33
NeuralNine
Рет қаралды 166 М.
The Ultimate Guide to Writing Classes in Python
25:39
ArjanCodes
Рет қаралды 117 М.
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 449 М.
Partial Functions in Python - Functools Tutorial
14:12
NeuralNine
Рет қаралды 16 М.
How To Use: "@dataclass" In Python (Tutorial 2023)
15:01
Indently
Рет қаралды 29 М.
Get your battery storage box
0:14
Nikola Toy
Рет қаралды 20 МЛН
Low Battery 🪫🥹🥹🥹
0:10
dednahype
Рет қаралды 4,8 МЛН