Hi all,

I'm looking at CPython's behavior when an addition is called. From what I understand, binary_op1 <https://github.com/python/cpython/blob/3.8/Objects/abstract.c#L786> is eventually called, and it calls either slotv or slotw, which seems to be the binaryfunc defined as nb_add in the field tp_as_number of respectively v / w.

I have a few questions:
1) In the default case, tp_as_number->nb_add is defined by the function slot_nb_add <https://github.com/python/cpython/blob/3.8/Objects/typeobject.c#L6312> itself stemming from the macro expansion SLOT1BINFULL <https://github.com/python/cpython/blob/3.8/Objects/typeobject.c#L6140> defined in typeobject.c. Both binary_op1(v, w) and slot_nb_add(v, w) appear to perform similar checks (if their second argument is a subtype of the first, etc), to decide if v's add or w's reverse add must be called and in which order. I find this repetition weird, and I guess I'm missing something... Any ideas?

2) From the SLOT1BINFULL macro, both __add__ and __radd__ are defined by the slot_nb_add function (with some argument swapping done by wrap_binaryfunc_l / wrap_binaryfunc_r). If I want to define a different behavior for the reverse operator during a definition with a PyTypeObject, I guess I should add an "__radd__" method?

3) If I create a user-defined class A, having different methods __add__ and __radd__, these methods are added in A's dictionary. From what I understand, the function update_one_slot <https://github.com/python/cpython/blob/3.8/Objects/typeobject.c#L7209> is then called to change A's tp_as_number->nb_add to point to the methods defined by __add__ and __radd__? From the code documentation, I think that "a wrapper for the special methods is installed". Where exactly is this wrapper applied, and how does it know when to dispatch to __add__ or __radd__?



Kind regards,
Raphaël
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/ZL3I4FSIRCB5QIIOOKNJPCFYMIKMECTO/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to