A Deep Dive into Python's Dataclasses (Part 1)

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ต.ค. 2024

ความคิดเห็น • 55

  • @TheAshutoshBhardwaj
    @TheAshutoshBhardwaj ปีที่แล้ว +19

    Hey Fred - You never fail to amaze your students. You are such a gifted teacher who can simplify anything and everything. Big kudos to you.

  • @FirasShamasnehM
    @FirasShamasnehM ปีที่แล้ว +2

    You are the best Fred, thank you very much for sharing your knowledge

  • @warrenmarkham8891
    @warrenmarkham8891 ปีที่แล้ว +1

    Am excited to watch this when I get the chance.🎉

  • @tannguyen_91
    @tannguyen_91 7 หลายเดือนก่อน

    I happy to hear your familiar voice, I didn't know what's your TH-cam channel before

  • @butterhalves4262
    @butterhalves4262 ปีที่แล้ว +2

    Even before starting the video I know that it's going to be awesome. Kudos Fred ! Would love a series on implementing various design patterns using python.

  • @drgregoryhouse3979
    @drgregoryhouse3979 10 หลายเดือนก่อน

    I just finished the udemy deepdive-1 and now half-way deepdive-2. But could not resist to watch this stand-alone course video right now. This is also a great video with the same level of detail as the deepdive series. Great added value !!! 😀

  • @kosmonautofficial296
    @kosmonautofficial296 ปีที่แล้ว +1

    Amazing video! That is a lot of great information. I thought I knew enough of dataclasses but learned a lot in this video. Thanks!

  • @Ilya_Akchurin
    @Ilya_Akchurin 11 หลายเดือนก่อน

    Oh! I can't resist to write a comment here! It was a very deep dive in dataclasses! Perfect material! Thank you very much!!!

    • @mathbyteacademy
      @mathbyteacademy  11 หลายเดือนก่อน

      glad you liked it, thanks!

  • @venkys15
    @venkys15 8 หลายเดือนก่อน

    Hey Fred - your courses are GOLD

  • @aoihana1042
    @aoihana1042 7 หลายเดือนก่อน

    Just Excellent!

  • @fatfrankie
    @fatfrankie ปีที่แล้ว +2

    Thanks for all the work you do. Your python lectures are best I've seen. Just as a suggestion... you should include a 30 second advert for your udemy courses at the start of each of your TH-cam videos. This would help those looking to learn Python as well as grow your Udemy following

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +1

      Thanks! Glad you like the videos. I do add them to the video description - not sure how people would tolerate a 30 second ad for my courses at the beginning of the videos - maybe I should look into cards instead...

    • @fatfrankie
      @fatfrankie ปีที่แล้ว +1

      @@mathbyteacademy Yes, displaying a card which links to an advert video (which gives a comprehensive description of your Udemy courses) is an excellent idea! I think very few people actually read the video descriptions. And experimenting with different approaches, and then analyzing their impact, will help you find the optimal approach. Good luck!

    • @fatfrankie
      @fatfrankie ปีที่แล้ว +2

      I think it might even be worthwhile creating a professional avert for your Udemy courses and then running it on TH-cam Ads

    • @JimRohn-u8c
      @JimRohn-u8c ปีที่แล้ว +3

      @MathByte Academy Fred your courses are some of the best in existence definitely promote them!

  • @modikai8507
    @modikai8507 ปีที่แล้ว

    Great lecture on dataclasees Fred, I especially enjoyed the comparison section with name tuples. Can't wait for the follow up video.

  • @valentinplechuc5710
    @valentinplechuc5710 ปีที่แล้ว

    Hi Fred, Great video,!! thanks for sharing the "in deep" of things.! I was wondering if sometime in the future you could do some comparison between attrs and dataclasses and when to use one or the other. Big kudos!!

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว

      Thanks! Yes, I am currently working on something like that, but it's taking me a while to come up with something that's not just superficial.

  • @StelioStefanov
    @StelioStefanov ปีที่แล้ว +1

    Great explanation

  • @DditsMas
    @DditsMas 5 หลายเดือนก่อน

    Great video thanks

  • @ashishsaraswat7867
    @ashishsaraswat7867 ปีที่แล้ว

    You are amazing Fred. Everytime I open youtube app, I check if you released new video. Awesome explanation. Can you please make videos on Machine learning concepts and maths required for machine learning?

  • @pavelekshin9261
    @pavelekshin9261 6 หลายเดือนก่อน

    Thanks! Did you start using dataclass more often? In which cases?

    • @mathbyteacademy
      @mathbyteacademy  6 หลายเดือนก่อน

      Not really. I use pydantic primarily, but that's because most of the projects I work on already have a dependency on pydantic. Otherwise I might use a dataclass, or maybe just a named tuple.

  • @packetporter
    @packetporter ปีที่แล้ว +1

    Hey Fred, great breadth and depth as always.
    A question regarding dataclasses sorting 47:15 , would using a __post_init__ method be an alternative to sort using radius and/or euclidean distance, for example
    ```
    @dataclass(order=True)
    class CircleD:
    sort_index: int = field(init=False, repr=False)
    euclidean_dist: float = field(init=False, repr=False)
    x: int = 0
    y: int = 0
    radius: int = 1
    def __post_init__(self):
    self.sort_index = self.radius
    self.euclidean_dist = dist((0,0), (self.x, self.y))
    ```

  • @duke007x3
    @duke007x3 ปีที่แล้ว +1

    Hi Fred, thank You for another great content and such explanation. Have a small question.
    24:20 You added private attributes x, y and radius. Should we also make them private in all other methods __repr__, __eq__ and __hash__? Because it looks like You did not make them private in those methods and in the video they only private in properties You add.

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +2

      Best practice is to use accessor methods if present. Sometimes you might want to circumvent the accessor methods and read/write the "private" backing variables, but most often you just use the accessors.

  • @sandeepsingavarapu3839
    @sandeepsingavarapu3839 ปีที่แล้ว

    I love your videos, amazing content in Udemy and TH-cam. Any plans of making a playlist on Data Structures and algorithms😀

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +3

      Thank you, glad you like them. No particular plans on data structures, but I do have a video on deques vs lists coming up next in the channel. I'll keep it in mind!

  • @vijaysingh-p3x
    @vijaysingh-p3x ปีที่แล้ว

    Hi Fred, thanks for creating these wonderful lessons. I am trying to understand how python is letting us allow a dictionary with duplicate keys (minute 23.35) in the video, what 's the explanation here, i was expecting the second circle to overwrite the first entry

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว

      Thanks, glad you like them. So dictionaries cannot have duplicate entries. The two objects you see there are different objects (if you could retrieve each one you'd observe that they have a different id, they started life out that way), but because we mutated one of the objects to now be equal (in terms of their == and hash) to the other, the two objects have different id's, but as far as the dictionary is concerned they are the "same" object and things get totally weird. Bottom line, you should not use mutable objects as keys of a dictionary.

  • @petervize
    @petervize ปีที่แล้ว

    Thanks!

  • @MindfreeLetsgo
    @MindfreeLetsgo ปีที่แล้ว +1

    Hi Fred, will you ever make a course on Udemy about making interesting projects using all different techniques of Python? I would love it!!

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +1

      I have thought about it many times - but I keep getting stuck on Windows vs Linux/Mac. I don't use Windows at all (haven't touched it in like over a decade), and a lot of the "real" projects I work on involve nix command line tools, including things like Makefiles, bash scripts, ci/cd pipelines, etc. I think a majority of Udemy students are on Windows, so that's why I keep getting stuck :-)

    • @MindfreeLetsgo
      @MindfreeLetsgo ปีที่แล้ว +1

      @@mathbyteacademy Ohhhh, that sounds more interesting to me now !! I want to learn bash scripts and Makefiles etc and I use Linux. Fred, I really hope that you somehow someway make that course on Udemy eventually. If not, I hope that you make it on youtube member channel so that those who want to learn it pay for it, I will definitely pay for that !

    • @duke007x3
      @duke007x3 ปีที่แล้ว

      @@mathbyteacademy Makefiles, bash scripts, ci/cd pipelines sounds really good, can we expect some video on this subject?)
      By the way what tools do you prefer to use for ci/cd pipelines?

    • @DifferentialGeometry
      @DifferentialGeometry 2 หลายเดือนก่อน

      @@mathbyteacademy Do it for the ones that are on Linux! :). I'm working at Microsoft and we use mainly Linux OS in Azure!

  • @RicardoSuarezdelValle
    @RicardoSuarezdelValle 5 หลายเดือนก่อน

    To be honest I didnt know about dataclasses so I was just using normal classes with __slots__, but 95% of what dataclasses do I was already doing with inheritance, only had to write the __repr__ and all that once, plus most of the dunder methods are useless. But yeah I will switch to dataclasses to avoid writing the __init__, but thats about it

    • @mathbyteacademy
      @mathbyteacademy  5 หลายเดือนก่อน +1

      What do you mean "most of the dunder methods are useless"? You mean like __eq__, __hash__, __gt__, __iter__, __next__, etc? If you think those are useless, may rephrase it to "useless to me", because I can assure you they are very useful and used extensively by Python devs (depending on the circumstances).

  • @RicardoSuarezdelValle
    @RicardoSuarezdelValle 5 หลายเดือนก่อน

    Im not an expert but I think your size comparisons are wrong. When u do the os.size or whatever, it only gives you part of the info on size, I seem to remember only like the reference to the class or something, i.e. a class with one attr will be the same size apparently as one with 100

    • @mathbyteacademy
      @mathbyteacademy  5 หลายเดือนก่อน

      Where do I use os.size? I'm looking at the code notebook, and doesn't look like I do.
      I am well aware of the limitations of os.size.

    • @RicardoSuarezdelValle
      @RicardoSuarezdelValle 5 หลายเดือนก่อน

      @@mathbyteacademy O sorry, maybe you did measured the sizes properly but then the sizes look wrong too because they are too similar. I mean in the table were you compare the sizes and speeds at the end of the video.

    • @mathbyteacademy
      @mathbyteacademy  5 หลายเดือนก่อน

      So, the library I use handles looking inside objects to get the full memory usage. Why are you surprised that the sizes are too similar? At the end of the day, named tuples are actual classes, dataclasses are well, regular classes, and slots do tend to be more efficient than not, but these are small classes, so i would expect difference to be quite small (in absolute terms), and indeed we can see the slots versions use less memory. The numbers I have in that table make sense to me.