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.
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.
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 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.
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.
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.
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
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.
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
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.
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...
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.
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.
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.
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.
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.
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 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).
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.
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)
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
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.
@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 🥺
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__`
@@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
👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis
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.
Thank you so much for the kind words, Aviel!
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
Thank you so much for your kind message!
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.
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!
Would you suggest any resources to do so?
@@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.
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.
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.
Omg this is content is AMAZING!!
It's everything I would want in an educational programming video.
Please keep using this format!
Thank you so much!
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
Thank you!
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.
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
Thanks a lot!
My favorite Python content. Thank you, Arjan.
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.
Me to
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...
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.
Donor methods, dominant methods, Donner methods, $1 methods, dama methods, Dhanam methods. Gotta love automatically generated subtitles.
You forgot the Donald methods.
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.
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.
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.
Massive like for the classic Batman reference alone!! Love your work, Arjan
4:20 also overwrite the __bool__ method for easy boolean expressions with the emulating class
Awesome video. I learnt something new today with dunder iterator. Thank you!
Great to hear!
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.
Haha. I did take a bit of creative freedom there as I really like the Batman transitions 😊.
"holy list comprehension!" - I want that on a t-shirt :)
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.
Thanks so much!
Hey Arjan, would love to see a video on sqlmodel !
I feel smarter, again, thanks!
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.
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.
You have the 'is' keyword. Doesn't that do what you're wishing for here?
@@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).
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.
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)
Me just realising DoubleUNDERscore is where "dunder" stems from 😂
Lovely!
Good morning sir, thanks for the new video and the comic cringe at the beginning. Let the comments fly. Smart... haha.
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
It’s not only higher order function it’s also closure:)
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.
@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 🥺
Consider me your virtual mentor! :)
A good addition to that vector class would be `__radd__` so a list of vectors can be passed to sum()
and __rmul__ for dilation.
Finally I know, where the word dunder comes from: "double underscored". 😀
hey Arjan, i really like your video's but the background music is a bit distracting
Hi, thanks for flagging it! :)
Um... "Marvel mode"... "Batman".... do you realise how many comic nerds you've probably just offended with that juxtaposition? ;)
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?
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__`
Probably because everyone will understand "number", but not everyone is equally familiar with IBAN, given that it's not used all over the world.
Also IBAN stands for International Bank Account Number. Either way you’re calling it a number
Donald methods
Dominant methods
Donor methods
Dolar methods
Dama methods
:D
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.
Batman is DC, not Marvel. ;)
I've been confused too
Using VS Code + iOS = telemetry
Are your video titles AI generated, or why am i so confused eveytime clicking on a german title but hearing english at the end? 😅
Yes, I included translations in 6 languages to provide more accessibility.
great video ! but batman is not part of marvel !!!
lol batman is DC
dunder methods are just builtin protocols. thats it.
I don't know why you translate your stuff with ai, but the translations are hilariously wrong. Just stop it, it's embarrassing.
I agree. Dunder is translated in dutch with: dollar, donor, donna and sometimes even with dunder 😂😮
Agree, doesn't bring anything.
Yea! Another example of why not to rely on the dumb ai ...
I'm trying to provide more accessibility; however, it's not possible for me to manually translate each video.
@@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