When to (Not) Use Dunder Methods?

  Рет қаралды 30,250

ArjanCodes

ArjanCodes

Күн бұрын

Пікірлер: 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 Жыл бұрын
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 Жыл бұрын
Thank you so much for the kind words, Aviel!
@matiasmercado3304
@matiasmercado3304 Жыл бұрын
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 Жыл бұрын
Thank you so much for your kind message!
@NicoPfeffer
@NicoPfeffer 3 ай бұрын
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
@ArjanCodes
@ArjanCodes 3 ай бұрын
Thank you!
@AllanHansen-n1d
@AllanHansen-n1d Жыл бұрын
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 Жыл бұрын
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.
@gmarcosoldo
@gmarcosoldo Жыл бұрын
Omg this is content is AMAZING!! It's everything I would want in an educational programming video. Please keep using this format!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much!
@gonecoastaltoo
@gonecoastaltoo Жыл бұрын
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.
@jeancerrien3016
@jeancerrien3016 Жыл бұрын
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.
@tikendraw
@tikendraw Жыл бұрын
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.
@akhiljp5352
@akhiljp5352 Жыл бұрын
Me to
@lukekurlandski7653
@lukekurlandski7653 Жыл бұрын
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...
@iandthopper
@iandthopper Жыл бұрын
My favorite Python content. Thank you, Arjan.
@thisoldproperty
@thisoldproperty Жыл бұрын
Awesome video. I learnt something new today with dunder iterator. Thank you!
@ArjanCodes
@ArjanCodes Жыл бұрын
Great to hear!
@the_coding_nick
@the_coding_nick Жыл бұрын
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
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks a lot!
@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.
@padraic1983
@padraic1983 Жыл бұрын
Hey Arjan, would love to see a video on sqlmodel !
@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.
@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
@MichaelRawson
@MichaelRawson Жыл бұрын
Massive like for the classic Batman reference alone!! Love your work, Arjan
@Hernell12
@Hernell12 Жыл бұрын
4:20 also overwrite the __bool__ method for easy boolean expressions with the emulating class
@sethstenzel
@sethstenzel Жыл бұрын
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 Жыл бұрын
Haha. I did take a bit of creative freedom there as I really like the Batman transitions 😊.
@jeffreymagedanz8130
@jeffreymagedanz8130 Жыл бұрын
Donor methods, dominant methods, Donner methods, $1 methods, dama methods, Dhanam methods. Gotta love automatically generated subtitles.
@__christopher__
@__christopher__ Жыл бұрын
You forgot the Donald methods.
@frantisekcastek174
@frantisekcastek174 Жыл бұрын
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.
@frackinfamous6126
@frackinfamous6126 Жыл бұрын
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.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much!
@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)
@cv7245
@cv7245 Жыл бұрын
"holy list comprehension!" - I want that on a t-shirt :)
@frozeneye100
@frozeneye100 Жыл бұрын
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.
@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 Жыл бұрын
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).
@TimoRJensen
@TimoRJensen Жыл бұрын
Lovely!
@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.
@frackinfamous6126
@frackinfamous6126 Жыл бұрын
Good morning sir, thanks for the new video and the comic cringe at the beginning. Let the comments fly. Smart... haha.
@RatafakRatafak
@RatafakRatafak Жыл бұрын
It’s not only higher order function it’s also closure:)
@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! :)
@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.
@ifeoluwaadeoye6557
@ifeoluwaadeoye6557 Жыл бұрын
Me just realising DoubleUNDERscore is where "dunder" stems from 😂
@edgeeffect
@edgeeffect 9 ай бұрын
Um... "Marvel mode"... "Batman".... do you realise how many comic nerds you've probably just offended with that juxtaposition? ;)
@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
@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?
@PiotrDrzymkowski
@PiotrDrzymkowski Жыл бұрын
Donald methods Dominant methods Donor methods Dolar methods Dama methods :D
@frantisekcastek174
@frantisekcastek174 Жыл бұрын
@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 Жыл бұрын
Consider me your virtual mentor! :)
@LittleGnawer
@LittleGnawer Жыл бұрын
Finally I know, where the word dunder comes from: "double underscored". 😀
@joekeenan2114
@joekeenan2114 Жыл бұрын
Batman is DC, not Marvel. ;)
@DenisTrebushnikov
@DenisTrebushnikov Жыл бұрын
I've been confused too
@topcivilian
@topcivilian Жыл бұрын
Using VS Code + iOS = telemetry
@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.
@BrigetteVazquezSegovia
@BrigetteVazquezSegovia Ай бұрын
great video ! but batman is not part of marvel !!!
@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 Жыл бұрын
Yes, I included translations in 6 languages to provide more accessibility.
@Bakobiibizo
@Bakobiibizo Жыл бұрын
lol batman is DC
@maksimon519
@maksimon519 Жыл бұрын
dunder methods are just builtin protocols. thats it.
@addcoding8150
@addcoding8150 Жыл бұрын
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 Жыл бұрын
I agree. Dunder is translated in dutch with: dollar, donor, donna and sometimes even with dunder 😂😮
@inopsek
@inopsek Жыл бұрын
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
Representing Monetary Values in Python
13:49
ArjanCodes
Рет қаралды 18 М.
Training Your Own AI Model Is Not As Hard As You (Probably) Think
10:24
Steve (Builder.io)
Рет қаралды 584 М.
Amazing remote control#devil  #lilith #funny #shorts
00:30
Devil Lilith
Рет қаралды 11 МЛН
Triple kill😹
00:18
GG Animation
Рет қаралды 18 МЛН
Walking on LEGO Be Like... #shorts #mingweirocks
00:41
mingweirocks
Рет қаралды 5 МЛН
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 287 МЛН
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 399 М.
Avoid These BAD Practices in Python OOP
24:42
ArjanCodes
Рет қаралды 70 М.
PYTHON MAGIC METHODS. __INIT__ и __NEW__
14:53
luchanos
Рет қаралды 6 М.
Protocol Or ABC In Python - When to Use Which One?
23:45
ArjanCodes
Рет қаралды 204 М.
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 119 М.
Python's magic methods
19:30
Carberra
Рет қаралды 12 М.
The Ultimate Guide to Writing Classes in Python
25:39
ArjanCodes
Рет қаралды 117 М.
Attrs, Pydantic, or Python Data Classes?
17:10
ArjanCodes
Рет қаралды 86 М.
__new__ vs __init__ in Python
10:50
mCoding
Рет қаралды 209 М.
Amazing remote control#devil  #lilith #funny #shorts
00:30
Devil Lilith
Рет қаралды 11 МЛН