When to (Not) Use Dunder Methods?

Поділитися
Вставка
  • Опубліковано 25 лис 2024

КОМЕНТАРІ • 84

  • @ArjanCodes
    @ArjanCodes  Рік тому

    👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis

  • @Aviel777Gergel
    @Aviel777Gergel Рік тому +3

    Python has a simple syntax, and this make people accidentally think that they good at it. Thanks to you, we can actually be good at it. Your content helped me to significantly improve my code quality.
    I hope your content will get much more views, because it super great, interesting and informative.

    • @ArjanCodes
      @ArjanCodes  Рік тому +1

      Thank you so much for the kind words, Aviel!

  • @matiasmercado3304
    @matiasmercado3304 Рік тому +6

    Hello Arjan. Thanks for your content, it helps me a lot to improve my skills. You are basically the senior I wish I had had

    • @ArjanCodes
      @ArjanCodes  Рік тому +2

      Thank you so much for your kind message!

  • @AllanHansen-n1d
    @AllanHansen-n1d Рік тому +4

    I think overwriting the __str__ and __repr__ is often a good idea, so IMHO those are exceptions to the rule about being careful overwriting the dunder-methods.

  • @kicknotes
    @kicknotes Рік тому +6

    One thing I've noticed is that devs will often learn a language by going through example exercises solving specific problems... While this is good, I've always found it better (for me, at least) to instead try to write code utilizing every element of a language (with the focus more on "coverage" than specific problems). In the case of Python, I made a point to write code implementing every single dunder method available in Python, and I think doing so was a *very* worthwhile effort (one that I'd suggest to other devs, in fact). Good video, as always!

    • @KAYounes
      @KAYounes Рік тому

      Would you suggest any resources to do so?

    • @kicknotes
      @kicknotes Рік тому

      @@KAYounes Really just the language documentation, or (if available) the source code to any standard library/framework.
      In the case of Python, I just looked up "data model methods" and started there. I also browse the source code of modules in the standard library and if I find something I've never seen before, I look it up, try to understand it, and then try to use it in my own code (even if just a one-liner).
      With C#, for example, I just dug through the .NET framework source code and did the same thing. Every time I found an operator (??, for example) or a keyword (unsafe, for example) that I hadn't seen/used, I made a point of writing a line or two of code that used what I just found.

  • @jeancerrien3016
    @jeancerrien3016 Рік тому +7

    Another problem with the example @13:06 is that one must modify the base class whenever you create a new subclass. One could use a factory method instead.

    • @rzimmerdev
      @rzimmerdev Місяць тому

      Yeah, he technically went halfway with the create_payment function, just needed to put it in a factory class. I think since he wasn't going to reference it elsewhere the function was correct for the use case specifically and much less heavy to understand for this video, but a factory method would be correct in any other scenario.

  • @gmarcosoldo
    @gmarcosoldo Рік тому +2

    Omg this is content is AMAZING!!
    It's everything I would want in an educational programming video.
    Please keep using this format!

  • @NicoPfeffer
    @NicoPfeffer 4 місяці тому

    Wow 😮 I really enjoyed the validator higher-order function section. I'll start implememting this. Thanks for your well made tutorials and the efforts. :D

  • @gonecoastaltoo
    @gonecoastaltoo Рік тому +4

    The __get__ dunder method is used to define a descriptor, which provides a nifty means of designing declarative syntax for composing complex object relations. This one is really easy to misuse or over-use, but used correctly it adds a super-power that I've not seen in other languages. @Arjan, maybe you could do a video on appropriate design and uses for descriptors and the __get__ dunder method? Most of the tutorials I've seen only scratch the surface and fail to reveal the power of this feature.

  • @the_coding_nick
    @the_coding_nick Рік тому +1

    Great job to Arjan for teaching Python information so well and accurately! Teaching a programming language like Python requires patience, clarity, and the ability to convey complex concepts in a simple and understandable language

  • @iandthopper
    @iandthopper Рік тому

    My favorite Python content. Thank you, Arjan.

  • @tikendraw
    @tikendraw Рік тому +2

    Hello arjan. I love watching you videos. Can you make video on handling paths while modularizing code. I am trying to find ways and haven't came across any good video explaining path handling.

  • @lukekurlandski7653
    @lukekurlandski7653 Рік тому +1

    Great video! While I do not disagree with the overall message, I slightly disagree with the example presented at 8:40. The two `validator' examples are not equivalent because the class-based option lets you mutate the validators while the function-based option does not. Therefore, there are conceivably some scenarios where the second solution would be much less efficient than the first...

  • @Andrumen01
    @Andrumen01 Рік тому

    Yes, your message is on point, I think. Use "dunder" methods if you really really really really need to! If there are less "really" statements, just don't use them. They make the code bulkier and more difficult to maintain. Yes, they are there to be used, but use them sparingly.

  • @jeffreymagedanz8130
    @jeffreymagedanz8130 Рік тому +1

    Donor methods, dominant methods, Donner methods, $1 methods, dama methods, Dhanam methods. Gotta love automatically generated subtitles.

  • @DrDeuteron
    @DrDeuteron Рік тому

    btw: if you want to do coordinate independent scalar products on your vectors, define a norm:
    def __abs__(self):
    return pow(self.x**2 + self.y**2, 1/2)
    and then:
    def __mul__(self, other):
    return (abs(self + other)**2 - abs(self - other)**2) / 4
    works on any vector with a norm, without regards to internals.

    • @__christopher__
      @__christopher__ Рік тому

      And wastes a lot of time doing a square root just to square it afterwards. I think this falls under the performance argument from the video.

    • @__christopher__
      @__christopher__ Рік тому

      Also a mathematical note: Unless the normal is defined by a scalar product to begin with, your "scalar product" won't have the properties of a scalar product. Which also violates the principle of least surprise.

  • @MichaelRawson
    @MichaelRawson Рік тому

    Massive like for the classic Batman reference alone!! Love your work, Arjan

  • @Hernell12
    @Hernell12 Рік тому +1

    4:20 also overwrite the __bool__ method for easy boolean expressions with the emulating class

  • @thisoldproperty
    @thisoldproperty Рік тому

    Awesome video. I learnt something new today with dunder iterator. Thank you!

  • @sethstenzel
    @sethstenzel Рік тому +14

    I don't know if you are trolling us or not, but Spiderman is Marvel, and Batman is DC :) Either way, love the reference. I, not so secretly, hope it was intentional just to annoy folks.

    • @ArjanCodes
      @ArjanCodes  Рік тому +7

      Haha. I did take a bit of creative freedom there as I really like the Batman transitions 😊.

  • @cv7245
    @cv7245 Рік тому

    "holy list comprehension!" - I want that on a t-shirt :)

  • @frackinfamous6126
    @frackinfamous6126 Рік тому +1

    Also funny thing. I watch you already but I was watching a comic video when this popped up. So.... you know what I'm saying. Google is def using auditory keywords. Keep up the great content.

  • @padraic1983
    @padraic1983 Рік тому

    Hey Arjan, would love to see a video on sqlmodel !

  • @frantisekcastek174
    @frantisekcastek174 Рік тому +1

    I feel smarter, again, thanks!

  • @ln11389
    @ln11389 Рік тому

    At 9:02, in c1_before.py line 6 typing.List should be used instead of default list class due to not being subscriptable on older python versions.

  • @markgyamfi2144
    @markgyamfi2144 Рік тому

    Thanks for the great video. The payment processor example would have been a great opportunity to flex with a match case besides the str enum. Or maybe thats already to old to boast with... darn that new release cycle😀. As a german when I see the __eq__ method I always think about the german words "dasselbe" and "das gleiche" which as a kid made me stumble. Maybe we need a __equal__ and __same__ method also in python to up the ante.

    • @Just2Dimes
      @Just2Dimes Рік тому +1

      You have the 'is' keyword. Doesn't that do what you're wishing for here?

    • @markgyamfi2144
      @markgyamfi2144 Рік тому

      @@Just2Dimes yes the is keyword checks for the reference. There is probably a way to implement a vector in a similar way as a simple number where assigned variables would have the same reference… but I dunno how or if there is a __same__ method that you could (would guess not).

  • @frozeneye100
    @frozeneye100 Рік тому +1

    Lol. Then I tell python Dundee dunder yourself a dunder str. It tells me I am an idiot because that is not way this works. If you guys need a lesson on how to make python scream at you. Please let me know. I can do Arjan in the opposite way.
    As always. Good video and you are one of few people that makes things sound simple but not giving the this is how to solve your problem but showing well how the things work. Very very well laid out and some still a bit beyond my skill level. But it is so good to see this type of thing and not a hello world program and really challenging to go read more documents. Well done as always for making it clear where there is room for self improvement.
    Depending where you are. Doing the time things difference I am astonished it is quicker way you did.
    But it does sort of feel like you start with functions then objects then class then get stuck dunder everything because it works. Just to take it all way back to what seems you were doing while learning. It really is a running on mill and explore and mess up so much. Unfortunately that idea still has not clicked because still exploring and trying to make ends of things.

  • @DrDeuteron
    @DrDeuteron Рік тому

    at 6:11, I would return type(self)(...). not Vector(...)...b/c you never know. I would also, to avoid writing more code do subtraction via:
    def __neg__(self):
    return type(self)(-self.x, -self.y)
    def __sub__(self, other):
    return self + (-other)

  • @ifeoluwaadeoye6557
    @ifeoluwaadeoye6557 Рік тому +4

    Me just realising DoubleUNDERscore is where "dunder" stems from 😂

  • @TimoRJensen
    @TimoRJensen Рік тому

    Lovely!

  • @frackinfamous6126
    @frackinfamous6126 Рік тому +2

    Good morning sir, thanks for the new video and the comic cringe at the beginning. Let the comments fly. Smart... haha.

  • @EW-mb1ih
    @EW-mb1ih Рік тому

    at 15:25, wouldn't be better to use a dict instead of the if-block in the function? The function would be more maintainable if we added new payment class

  • @RatafakRatafak
    @RatafakRatafak Рік тому

    It’s not only higher order function it’s also closure:)

  • @bigutubefan2738
    @bigutubefan2738 Рік тому

    You could just use contextlib instead of dunder methods for DatabaseConnection. I know inheritance is unpopular and composition is favoured, but even Bank would be greatly simplified by making it a subclass of a list, instead of messing around with dunders.

  • @frantisekcastek174
    @frantisekcastek174 Рік тому +2

    @All: Guys, do you have a senior that is explaining the coding stuff like this to you, leading you to evolve faster? I am missing this in our company. I am left on my own, but I could grow so much faster having somebody like Arjan giving me feedback 🥺

    • @ArjanCodes
      @ArjanCodes  Рік тому +1

      Consider me your virtual mentor! :)

  • @codingwithmrward1648
    @codingwithmrward1648 Рік тому

    A good addition to that vector class would be `__radd__` so a list of vectors can be passed to sum()

    • @DrDeuteron
      @DrDeuteron Рік тому

      and __rmul__ for dilation.

  • @LittleGnawer
    @LittleGnawer Рік тому

    Finally I know, where the word dunder comes from: "double underscored". 😀

  • @johantoday
    @johantoday Рік тому

    hey Arjan, i really like your video's but the background music is a bit distracting

    • @ArjanCodes
      @ArjanCodes  Рік тому

      Hi, thanks for flagging it! :)

  • @edgeeffect
    @edgeeffect 9 місяців тому +1

    Um... "Marvel mode"... "Batman".... do you realise how many comic nerds you've probably just offended with that juxtaposition? ;)

  • @appuser
    @appuser Рік тому

    18:00 but isn't that just because of the way the dunder method was used, not really to do with it being a dunder method?

  • @ipadista
    @ipadista Рік тому

    As always interesting vid!
    4:03 in your Account class, why do you call the account ID "number"? Nowadays all banks use IBAN as the account ID, so iban would be a more relatable name both in that class and the Bank class iban would also be an excellent thing to display in `Account.__repr__`

    • @Just2Dimes
      @Just2Dimes Рік тому

      Probably because everyone will understand "number", but not everyone is equally familiar with IBAN, given that it's not used all over the world.

    • @RileyGein
      @RileyGein Рік тому

      Also IBAN stands for International Bank Account Number. Either way you’re calling it a number

  • @PiotrDrzymkowski
    @PiotrDrzymkowski Рік тому

    Donald methods
    Dominant methods
    Donor methods
    Dolar methods
    Dama methods
    :D

  • @rosyidharyadi7871
    @rosyidharyadi7871 Рік тому

    I've known python for years but just realizing dunder stands for double underscore. Not a native, so I thought it was a real English word.

  • @joekeenan2114
    @joekeenan2114 Рік тому +2

    Batman is DC, not Marvel. ;)

  • @topcivilian
    @topcivilian Рік тому

    Using VS Code + iOS = telemetry

  • @RealLexable
    @RealLexable Рік тому

    Are your video titles AI generated, or why am i so confused eveytime clicking on a german title but hearing english at the end? 😅

    • @ArjanCodes
      @ArjanCodes  Рік тому +1

      Yes, I included translations in 6 languages to provide more accessibility.

  • @BrigetteVazquezSegovia
    @BrigetteVazquezSegovia Місяць тому

    great video ! but batman is not part of marvel !!!

  • @Bakobiibizo
    @Bakobiibizo Рік тому

    lol batman is DC

  • @maksimon519
    @maksimon519 Рік тому

    dunder methods are just builtin protocols. thats it.

  • @addcoding8150
    @addcoding8150 Рік тому +2

    I don't know why you translate your stuff with ai, but the translations are hilariously wrong. Just stop it, it's embarrassing.

    • @franktewierikholscher
      @franktewierikholscher Рік тому +1

      I agree. Dunder is translated in dutch with: dollar, donor, donna and sometimes even with dunder 😂😮

    • @inopsek
      @inopsek Рік тому +1

      Agree, doesn't bring anything.

    • @cezartorescu
      @cezartorescu Рік тому

      Yea! Another example of why not to rely on the dumb ai ...

    • @ArjanCodes
      @ArjanCodes  Рік тому

      I'm trying to provide more accessibility; however, it's not possible for me to manually translate each video.

    • @addcoding8150
      @addcoding8150 Рік тому

      @@ArjanCodes That's totally understandable.
      But you ain't increasing accessibility when the translations are grammatically and semantically wrong.
      I'd rather have 0 subs than wrong subs