In Python, I can overload the __add__ method of an object (or other aka "dunder" double underscore methods). This allows me to define custom behavior for my objects when using Python statements.
Is it possible to find out from the dunder method if the method was called via + or via __add__ ?
For example, suppose I want to create an object that prints "+" or "__add__" , depending on whether + was used or if __add__ was called.
class MyAdder(object): def __add__(self, other): print method_how_created() return 0 MyAdder() + 7
The inability of any magic such as method_how_created , is there canonical mapping of characters for dunder labels? I know that there are lists such as http://www.python-course.eu/python3_magic_methods.php or solutions based on the analysis of the docstrings of the operator module, as indicated here: Access operator functions by symbol . Is there a way to find a map between function names and characters that are slightly less hacked than parsing documents or manually creating a list?
python
cjrieds
source share