Magic Methods, R Methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Magic Methods, R Methods

I now understand the concept of Magic Methods. On a slide, it says as an example, x + y is translated into x.__add__(y). However, if x hasn't implemented __add__, and x and y are of different types, then y.__radd__(x) is called. It also says there are r methods for all magic methods mentioned, such as _sub_, _or_, _xor_ etc. But as I read on, I see comparison methods like _lt_ and _contains_ for in. Do r methods apply to them as well?

25th Dec 2018, 9:25 PM
BitsAndBytes
BitsAndBytes - avatar
1 Answer
0
Hi I think your question is quite intriguing, and actually I found a rather comprehensive explanation of magic methods https://rszalski.github.io/magicmethods/ However, found no proof of what you actually asked :) That would be a fun OOP project to test this hypothesis with two different custom classes and customised comparison operators. If you found the answer in the meantime, I would be very interested to hear!
8th Jan 2019, 7:40 PM
Tibor Santa
Tibor Santa - avatar