+ 6
Question about magic methods.
There's this chapter in the Python course which says x+y is translated as x.__add__(y). So I just wanted to know if x.__add__(y) is faster than x+y, and if so, why? Thanks! https://www.sololearn.com/learn/Python/2470/
3 Answers
+ 6
Thanks Rahul Hemdev !
Please visit:
https://code.sololearn.com/cZE9OIep89O0/?ref=app
I wish you'll get the answer there.
Please if anybody know why one is better than the other then please reply.
+ 4
Brian R Yes, I agree with you.
Faheem Hossain Your code was really helpful... But I too am wondering about the same thing.
Edit: I found this https://stackoverflow.com/questions/21734658/performance-discrepancy-obj-setitem-x-y-vs-objx-y from Stack Overflow, which is kind of related to this question... But still, I couldn't find a satisfactory answer.
+ 3
to be honest im not sure which is actually faster. if i had to guess, its the exact same. if one method was faster than the other, the difference is so small that it doesn't actually make a difference in the run time of your program. you should refrain from calling "magic methods" directly because python has better methods of doing these things that are more "pythonic". remember: python emphasizes readability, so whatever seems easier to understand when reading the program is probably the "right way" of doing it.