If You’re Not Using Python DATA CLASSES Yet, You Should 🚀

Поделиться
HTML-код
  • Опубликовано: 15 окт 2024

Комментарии • 420

  • @ArjanCodes
    @ArjanCodes  3 года назад +341

    Anybody else into The Witcher?

    • @Newascap
      @Newascap 3 года назад +6

      If you talking about the game, I bought it on gog but never got the chance to install it. Too much time fighting with k8s manifests and no time for playing :c

    • @ArjanCodes
      @ArjanCodes  3 года назад +8

      I have the Witcher 3 for the Playstation lying in a drawer, haven't gotten around to that either.

    • @draufunddran
      @draufunddran 3 года назад +1

      @@ArjanCodes I have the Witcher 3 also lying in a drawer but mine is for the xbox :D

    • @leftblank5315
      @leftblank5315 3 года назад

      I thought the show had a bit of corny dialog but the more i watched it the more i was immersed and it didn't seem bad. so yeah i love it

    • @itsmemouha2811
      @itsmemouha2811 3 года назад +2

      You've got to read the books, a masterpiece. If you liked the TV show or didn't, it won't matter after reading just the first two short story collection.

  • @falak88
    @falak88 3 года назад +157

    Just in case you're feeling low Arjan, you are doing a freaking great job here. Thank you very much ! Keep them coming :D

  • @drymanic
    @drymanic 3 года назад +63

    One cool thing about the "frozen" attribute is that if set to True, Python will automatically create a ___hash___ function for your dataclass, allowing it to be used in things like dictionary keys and sets.
    (Note that if the class contains an unhashable field such as a list, the ___hash___ function will throw an exception).

  • @CodingEntrepreneurs
    @CodingEntrepreneurs 3 года назад +32

    This is great! Thanks for sharing Arjan. Love your work.

  • @JohnMitchellCalif
    @JohnMitchellCalif 3 года назад +12

    Incredibly clear presentation. I've been programming Python for 25 years and hadn't used data classes. Thanks! Subscribed.

  • @dennissmith6867
    @dennissmith6867 3 года назад +44

    This is a really great intro to data classes, very clear and to the point. Looking forward to more videos like this!

    • @ArjanCodes
      @ArjanCodes  3 года назад +1

      Glad you liked it, Dennis!

  • @myce-liam
    @myce-liam Год назад +2

    Arjan, your videos are really clear and simple. You are very much appreciated. Greetings from the UK!

    • @ArjanCodes
      @ArjanCodes  Год назад +1

      Thanks so much Liam, glad the content is helpful!

  • @Bc7-w9k
    @Bc7-w9k 2 года назад

    I LOVE U, THIS IS THE FIRST STEP FOR UNDERSTAND FAST API AND PYDANTIC,
    THANK U FOR URS VIDEOS

  • @mithunmanoharmithun
    @mithunmanoharmithun 2 года назад +4

    I would love to see a complete python course taught by Arjan!

  • @jimmy21584
    @jimmy21584 3 года назад +92

    When dealing with data in Python these days, I find myself almost exclusively doing comprehensions and functional programming with dictionaries and lists, rather than using classes. But good to know this is out there.

    • @ZergD
      @ZergD 3 года назад +3

      I concur with that!

    • @AddyVDH
      @AddyVDH 3 года назад +1

      This is the best way IMO. Heavy OOP often makes python code super messy as many people get involved in a codebase.

    • @bakane6030
      @bakane6030 3 года назад +2

      @@AddyVDH Agreed. Python is NOT supposed to be an object oriented language

    • @nsambataufeeq1748
      @nsambataufeeq1748 3 года назад

      I had a graduate class in LISP, i found the functional bits interesting and are now using them in python

    • @JustMaiyak
      @JustMaiyak 3 года назад

      FP un Python feels great !

  • @alicebrown854
    @alicebrown854 2 года назад

    Where is 'love' button? 'Like' is not an accurate reflection of what I feel about your videos. I've seen many amazing dudes with online tutorials on RUclips, but you are the most amazing

  • @Yassou1985
    @Yassou1985 9 месяцев назад

    Arjan keep doing your video man ! Your are a true teacher, everthing you say is just so clear. Thank you for your help :)

    • @ArjanCodes
      @ArjanCodes  9 месяцев назад

      I'm glad my content has been helpful! Thank you for the kind words :)

  • @typehint
    @typehint 2 года назад +6

    Really happy I found your channel! You and Corey Schafer are my two favourites for learning Python right now.
    Keep up the excellent work!

  • @niveshsanghvi9066
    @niveshsanghvi9066 3 года назад

    Nice video Arjan ! Looks very useful

  • @gedtoon6451
    @gedtoon6451 9 месяцев назад

    I find the content of your videos are on the edge of my python knowledge. When I watch one, I learn something new, without it making my brain hurt!

    • @ArjanCodes
      @ArjanCodes  9 месяцев назад

      That's a good way to put it! Ahah, I'm glad you're enjoying the content!

  • @antoniopena1183
    @antoniopena1183 2 года назад

    Very straightforward and nice way of explaining how it all works.

  • @skyletwings5711
    @skyletwings5711 Год назад

    8:43 "So frozen helps you to make sure the data is not changed anywhere in your code." ... right after bypassing this mechanism and showing how the data is changed in the code. :D
    Great videos, you are my no. 1 python instructor. Programming python itself is fun, but learning from a patient, calm, well explaining teacher is invaluable, but free. Thank you for sharing!!

  • @rupeshchoudhary9237
    @rupeshchoudhary9237 2 года назад +1

    Great job. I love you explaining style.

  • @martindu9083
    @martindu9083 2 года назад

    good video, for sort , dataclass will use all attributes by order for checking, so sort_index is not mandatory, its effected just because its the first one

  • @nlight8769
    @nlight8769 2 года назад

    I've been coding for... hmm... since python 2.5 as an amateur, when I need it. The vast majority of what I learned is from back then, and while i've been keeping learning, in the last years, besides some modules here and there, I've not learned many new things... discovered the chanel last week, and man I've learned so much since ! Sure there are few design pattern that I was already doing out of pure logic and commodity, though even then the knowledge acquired here have allowed me to refine these to the next level (or on the road to it), plus the terms that goes with it ! Thank you so much !
    And there are many new neat things that I've been missing, like the module abstractclass or this dataclass.
    A great channel : advanced and intermediate concepts, in python !

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Thank you, I'm happy you like the content!

  • @theophilegaudin2329
    @theophilegaudin2329 3 года назад +1

    I started using classes when I could not do otherwise.
    My fonctions had like 15 variables that I had to state explicitly. It was impractical. My code was working on molecules and mixtures of molecules. So I created a class "mixture" and a class "molecule" and suddenly my fonctions were acting on mixtures and molecules rather than long lists of variables.
    That was such a relief.
    Now my classes have like 70 attributes because, well, mixtures and molecules have many structural features and properties. Everytime I extend my code I simply add new attributes and that's it.

  • @its_murwayi
    @its_murwayi 2 года назад

    This guy is awesome . The stuff i pick from you everyday is priceless. Got people at work thinking am a Python god

  • @estevaoyt
    @estevaoyt 9 месяцев назад

    Arjan, you are great inspiration, love your videos!

    • @ArjanCodes
      @ArjanCodes  9 месяцев назад

      I'm happy to hear you have been enjoying the videos!

  • @AdityaDiwakarVex
    @AdityaDiwakarVex 3 года назад

    Video quality is insanely good for only 8K subscribers. Keep it up!

  • @RonaldPostelmans
    @RonaldPostelmans Год назад

    Hoi Arjan. Je maakt mooie video’s met duidelijke uitleg. Kun je een praktisch voorbeeld geven waarvoor jij data classes zou gebruiken. Ik zou denken dat, je die niet nodig hebt als je bv crud operaties wilt doen naar b.v. een SQL database

    • @ArjanCodes
      @ArjanCodes  Год назад +1

      Hi Ronald, dankjewel! Dataclasses zijn erg handig in combinatie met Pydantic, aangezien je dan gebruik kunt maken van validatie van velden voor bijvoorbeeld APIs of database-modellen. Het is ook handig voor het representeren van een set config-waarden die je uit bijvoorbeeld een JSON file leest.

  • @ldebrotb09
    @ldebrotb09 2 года назад

    Thank you Arjan! Your videos about how to make more of Python (with built-in functions), write better and cleaner code, etc. motivate me in trying to become more Pythonian.

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Glad to hear they’re helpful to you, Lucien!

  • @sf-spark129
    @sf-spark129 Год назад

    I've been using the conventional class in Python. This is such a great knowledge boost!!

  • @aar021
    @aar021 3 года назад +3

    That intro was hilarious. Well put video. Super clear.

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Glad you liked it Alvaro!

  • @letsaram
    @letsaram 3 года назад +2

    I love how wisely you avoid all the issues I've had with other peoples code.

  • @ikustudies
    @ikustudies Год назад

    you're making me rethink my entire codebase. i wish i could start over now but i'm so far. sheesh

  • @hlore7600
    @hlore7600 Год назад

    Amazing! Thank you for creating this content!

  • @christopherc4526
    @christopherc4526 Год назад

    Just found your channel and I am glad I did. Outstanding introduction to very useful concepts. Well done

  • @python_byte
    @python_byte 3 года назад

    Just loving all of your videos man, keep up the awesome work. My focus is to watch ALL THE VIDEOS

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thanks, glad you like the videos!

  • @cetilly
    @cetilly 3 года назад +10

    Awesome! Coming from a C# background this really makes my day to know data classes are a thing in Python. Awesome. Really good video. Thanks Now I need to refactor all my custom data classes :-D

    • @ArjanCodes
      @ArjanCodes  3 года назад +3

      It seems technical debt never really stops 😊.

  • @DanielRios549
    @DanielRios549 3 года назад +4

    This is something I was wondering to use in Python since I discovered that in PHP 8, you do not need to set the construct parameter to attributes anymore, you set the attributes directly on the parameter, this is very useful when you have a lot of initial parameters, pass the parameter and after that set it to an attribute is so so boring

  • @gregoryfenn1462
    @gregoryfenn1462 3 года назад

    I really like this! It feels like modern python is much more robust and can be better self-documented and typed nowadays.

    • @ArjanCodes
      @ArjanCodes  3 года назад

      You might also like Pydantic. It’s very similar but adds a few extras like data validation and nested models. It is a third-party package though.

  • @talhaamir9023
    @talhaamir9023 3 года назад

    I loved the start of the video :)
    This channel deserves a million subs,the only channel most quality content :)

  • @FRANKWHITE1996
    @FRANKWHITE1996 3 года назад

    Great video. Thanks for sharing.

  • @thatinstant
    @thatinstant 3 года назад

    Wow! This video was super helpful in getting familiar with data classes! Great intro! ...Subscribed!

  • @Egon3k
    @Egon3k 3 года назад +2

    Everytime watching one of your videos, I learn something new. Keep on going with these very nice and easy to understand videos :)

  • @kruceo
    @kruceo 3 года назад +5

    Clearly demonstrated. Earned my “like and subscribe”! Looking forward to checking out your other tutorials/content.

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thank you Marcel - glad you liked the content!

  • @fabulousfabio8228
    @fabulousfabio8228 2 года назад

    Thanks for the video. I just learnt something!

  • @VoyivodaFTW1
    @VoyivodaFTW1 3 года назад

    That HHKB. Typing was so fast and smooth.

    • @ArjanCodes
      @ArjanCodes  3 года назад

      It's a Keychron K2. Feels great.

  • @leanbravo8856
    @leanbravo8856 3 года назад

    Why did I take so long to discover this channel? True goldmine of knowledge. Hit the bell.

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thank you Lean, glad you like the content!

  •  3 года назад

    Awesome tutorial bro!
    I had used data classes but this is much more details that I didn’t know about.
    Thanks

    • @ArjanCodes
      @ArjanCodes  3 года назад +1

      You’re welcome- glad you enjoyed it!

  • @andrewiglinski148
    @andrewiglinski148 2 года назад

    Dude I'm a pretty experienced developer but brand new to python as of 2 months ago... I've seen like 5 videos on dataclasses by now and this is the first one that actually showed the benefits over regular classes... after by mile long init function is finished lol

  • @matthewbailey3052
    @matthewbailey3052 3 года назад

    Very useful, thanks!

  • @qrubmeeaz
    @qrubmeeaz 2 года назад +1

    Best Python videos in the known universe!!

  • @PNadav183
    @PNadav183 2 года назад

    quick question about the __str__ representation you added, why did you use __str__ instead of __repr__? I usually choose __repr__ when it doesn't really make sense that an object be casted to a string but I still want to print it (for debugging or just to display it for some other reason) and I use __str__ when I think there is a relevant use case for a string casting.
    But these were my own conclusions and a convention I set for myself, is this a good practice? Is there any reason to use __repr__ over __str__ (or vice-versa)?

  • @pranavnyavanandi9710
    @pranavnyavanandi9710 2 года назад

    Long since I subscribed. 😊 This is quite the top notch content.

  • @kyletech4878
    @kyletech4878 3 года назад +1

    I find the asdict and astuple methods from the dataclass library very handy.

    •  3 года назад +1

      They are, I often use asdict to convert a dataclass to JSON :)

  • @aurielklasovsky1435
    @aurielklasovsky1435 3 года назад

    Oh my, that is a great product! I wish there was a way to pre order it though... It seems to ve impossible atm🤔

  • @Xaminn
    @Xaminn 3 года назад +1

    Damn. This information is going to save me years of time and frustration. Thanks for this.

  • @johnniedalesandro3269
    @johnniedalesandro3269 Год назад

    These are EXCELLENT videos.

  • @mikelancaster8924
    @mikelancaster8924 Год назад

    Thanks, great job!

  • @patrikwiklund6154
    @patrikwiklund6154 3 года назад +2

    Arjan. I really appriciate your videos! Thank you! Really awesome

  • @AJ-et3vf
    @AJ-et3vf 2 года назад

    Awesome video! Thank you!

  • @Mjuziks
    @Mjuziks 2 года назад

    it was very useful, thanks

  • @hoshiataru
    @hoshiataru 2 года назад

    I tried making a playing card dataclass but since the comparison is dependent on the tuple of all values I had to override the comparison operators or "Ace of Hearts is > Ace of Diamonds (H > D)".
    I'm really new to Python (and programming), I thought dataclass would be good for playing card class as it's just a data holder anyway but if I ended up implementing my own sorting is it still worth using? Overhead?
    It's such a simple use case maybe I'm over complicating things? I think my only benefit was the repr, str, and the init having done for me.

    • @PanduPoluan
      @PanduPoluan Год назад

      Maybe check also the attr.s package.

  • @davidbacter5424
    @davidbacter5424 3 года назад

    Most of the programming channels show basic information with less relevant examples. I just came across this channel and let me tell you that you share the most relevant information I have ever seen on any channel. Thank you for your work, I really appreciate it!

  • @pippog88
    @pippog88 3 года назад +1

    Hands dowm the best tutorials on Python! Thanks for what you're doing! :)

  • @sergiopovoli4332
    @sergiopovoli4332 3 года назад

    Thank you sir for this video! It was very helpfull for me.

  • @disko.kommando
    @disko.kommando 3 года назад

    Great video though I am struggling to find my own personal use cases. Seems unpythonic in syntax to me?

  • @glebsidorov9647
    @glebsidorov9647 3 года назад

    Thanks for the class Mr :)

  • @it_is_ni
    @it_is_ni 3 года назад

    Another small thing: I see you using both single and double quotes. If you use a formatter (like Black) and set VSCode to Format on Save you’ll automatically get rid of these inconsistencies.

    • @ArjanCodes
      @ArjanCodes  3 года назад +1

      In my more recent videos, I’ve started using a combination of Pylance, Pylint and Black - works really well!

  • @Gajet9
    @Gajet9 3 года назад +1

    I do understand that you used sort_index to showcase field types, but wouldn't it be more intuitive to define sort_index to be a property? That way we ensure it's readonly and we also ensure the sort_index updates automatically™ as the relevant fields are updated.

  • @TommyReady
    @TommyReady 2 года назад

    Great video, I learned something.

  • @VorpalForceField
    @VorpalForceField 2 года назад

    very cool .. Thank You for sharing

  • @justchary
    @justchary 3 года назад

    Very good! Thank you. You really have a talent to explain things

  • @vitorcontierirezendepecanh1165
    @vitorcontierirezendepecanh1165 3 года назад

    Fantastic video, thank you

  • @sami3592
    @sami3592 2 года назад

    Another great video. Thanks a lot.

    • @ArjanCodes
      @ArjanCodes  2 года назад

      You’re welcome, glad you liked it!

  • @nowyouknow2249
    @nowyouknow2249 2 года назад

    You are my python role model

  • @Ezechielpitau
    @Ezechielpitau 3 года назад

    very clear, good length, witcher references. what's not to like? subscribed :)

  • @sengatenga8552
    @sengatenga8552 2 года назад

    Hi Arjan, you have really great videos and great code. Can you do any video on database connection to python as OOP? Would love to see how you do it. Keep up the good work!

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Thanks Senga, happy you’re enjoying the content!
      Thanks for the suggestions, I've put it on the list.

  • @joshcousins9422
    @joshcousins9422 3 года назад +3

    Such awesome content, this video got me to start learning python and I'm loving it! Big request though, could you do a video about Flask? Would be much appreciated!

    • @ArjanCodes
      @ArjanCodes  3 года назад +2

      Thank you Josh - it’s on the list 😉.

    • @peitlijozsef
      @peitlijozsef 3 года назад

      @@ArjanCodes I hope with Dash...

    • @aadithyavarma
      @aadithyavarma 3 года назад

      @@ArjanCodes Can you compare Flask with FastAPI too? Seems like FastAPI is faster and is more pythonic in way, but I am unsure of the drawbacks of choosing FastAPI over Flask. Can you share your thoughts?

  • @techcode_man
    @techcode_man 2 года назад

    Just awesome!! Thank you

  • @ShanilPanara
    @ShanilPanara 2 года назад

    Loved this!

  • @GJ22
    @GJ22 3 года назад +4

    All of your videos are still a little above my aptitude, I'm still relatively new to coding in general but I feel jumping in at the deep end can sometimes be good 👍

  • @diogosilva3152
    @diogosilva3152 3 года назад

    I have been programming in Python for quite some time now, and I had no idea of this functionality. Thanks :p

  • @retrofutur1st
    @retrofutur1st 3 года назад

    10/10 video, subscribed and liked 😀

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thank you, glad you liked it!

  • @ayzack2361
    @ayzack2361 3 года назад

    Nice video, man! I like your videos.

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thanks! Glad you like them.

  • @manonthedollar
    @manonthedollar 3 года назад +26

    I've never been totally sold on data classes. To me, the functionality has been available forever in the form of dunder methods, @property decorators, and such. Am I being unreasonable here? Is there any sort of performance gain to data classes? Thanks for the excellent video as always!

    • @ArjanCodes
      @ArjanCodes  3 года назад +37

      Thanks! Actually, data classes are exactly the same as regular classes. The only thing the decorator does is already add dunder methods to make the class more suitable for dealing with representing data. So it's basically a shorter version of adding dunder methods yourself. Obviously, if what the data class decorator adds doesn't fit with what you need, then it is better to define those methods yourself, there is no particular performance gain to data classes.

    • @mikeciul8599
      @mikeciul8599 2 года назад

      Can you make sort_index a @property so you don't have to mess around with mutation at all?

  • @fl4tcircl3
    @fl4tcircl3 Год назад

    Thank you sir, brilliant!

  • @Orionrobots
    @Orionrobots Год назад

    Could that sort order field be made as a python property? Do we get some shortcuts around using slots for packing these in memory when in large arrays?

  • @elvistsang832
    @elvistsang832 3 года назад

    This is amazing! I’ve been struggling with my project and this totally saved my day!

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Glad I could help, Elvis!

  • @marcelohpinheiro
    @marcelohpinheiro 3 года назад

    Thanks for your video!

  • @PanduPoluan
    @PanduPoluan Год назад

    Ahahahah, the opening where you're presenting dataclasses as if they're a sponsor, that's hilarious 😂

    • @ArjanCodes
      @ArjanCodes  Год назад +1

      Thank you Pandu, glad you liked the video!

  • @simplepycodes
    @simplepycodes 2 года назад

    Perfect. Thank you.

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Thanks so much, glad you liked it! :)

  • @psykowarior
    @psykowarior 3 года назад

    Amazing !
    Love it, share it.

  • @zacky7862
    @zacky7862 3 года назад

    @ArjanCodes
    I wanna ask a little.
    Is it ok if I can do like this
    @dataclass
    class Test:
    my_list = ["item1", "item2", "item3"]
    or there's a better approach for that?

    • @ArjanCodes
      @ArjanCodes  3 года назад +1

      It's not allowed to initialize a list directly like that with dataclasses. What you can do instead is rely on the __post_init__ method, and initialize the list there, as follows:
      @dataclass
      class Test:
      my_list: list[str] = field(init=False)
      def __post_init__(self):
      self.my_list = ["item1", "item2", "item3"]
      By adding the "field(init=False)" you don't have to provide a value for the list when you create an instance, so now you can do this:
      test = Test()
      print(test)
      # prints: Test(my_list=['item1', 'item2', 'item3'])

    • @zacky7862
      @zacky7862 3 года назад

      @@ArjanCodes Finally! thank you very much. This is very helpful

    • @zacky7862
      @zacky7862 3 года назад

      @@ArjanCodes This is much better than I found on internet
      my_list: list = field(default_factory=lambda: ["item1", "item2"])

    • @ArjanCodes
      @ArjanCodes  3 года назад

      That’s a nice solution too!

    • @zacky7862
      @zacky7862 3 года назад

      @@ArjanCodes Your solution is much better :) Because it's clean and I'm storing many fields with list

  • @tuxhome3686
    @tuxhome3686 3 года назад

    Great video! I learned a lot.

  • @drheaddamage
    @drheaddamage 9 месяцев назад

    It's funny that the main touted advantage of Python is that it's got dynamically typed variables. And now the first thing the seasoned programmer does is *hammer those variables down to be a single type to avoid issues further down the road* 😁

  • @brettbyrnes577
    @brettbyrnes577 3 года назад

    Nice video! I’m subscribed now!

  • @apachethehun
    @apachethehun 3 года назад

    Thanks for sharing. Love this approach. Do you think dataclasses can be used for comparing two data sets. While end of records->generate a dataclass for that row and do a comparison between each row?

    • @ArjanCodes
      @ArjanCodes  3 года назад +1

      You’re welcome! This is certainly possible. If you have more elaborate needs, such as nested data structures or need to validate the data when you create the objects, you might want to take a look at Pydantic.

  • @crosby3108
    @crosby3108 3 года назад

    This was amazing thanks for going into so much detail

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thanks - glad you liked it!

  • @abrahamvivas9540
    @abrahamvivas9540 3 года назад +1

    And this is how Python get a little bit closer to Haskell... Nice!

    • @YASYTU
      @YASYTU 3 года назад

      But then why don't we just use Haskell?

  • @coriariasgibert9945
    @coriariasgibert9945 3 года назад

    Thanks 😃 great video !

  • @ranaanas-cl2cj
    @ranaanas-cl2cj 3 года назад

    love ur work thanks please continue
    we need you

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thank you so much! No worries, I will :).

  • @phen2841
    @phen2841 3 года назад

    Your keyboard sounds so nice! Did you mod your k2 at all or is it just the switch?

    • @ArjanCodes
      @ArjanCodes  3 года назад +1

      No, it's completely stock (with the aluminium frame though, perhaps that changes the sound a bit).

  • @croniqqGhost
    @croniqqGhost Год назад

    Good twist at the start