+ 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/

12th Nov 2020, 3:11 AM
Rahul Hemdev
Rahul Hemdev - avatar
3 Respuestas
+ 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.
12th Nov 2020, 5:48 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 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.
12th Nov 2020, 5:35 PM
Rahul Hemdev
Rahul Hemdev - avatar
+ 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.
12th Nov 2020, 3:18 AM
Brian R
Brian R - avatar