Calling dunder methods manually

eryk sun eryksun at gmail.com
Thu Apr 13 11:11:25 EDT 2017


On Thu, Apr 13, 2017 at 8:24 AM, Chris Warrick <kwpolska at gmail.com> wrote:
> On 13 April 2017 at 09:43, eryk sun <eryksun at gmail.com> wrote:
>> The functions in the operator module implement abstract behavior (e.g.
>> PyNumber_Add in CPython):
>>
>>     >>> operator.__add__(C(), D())
>>     42
>
> Those functions also do not need underscores — operator.add is a
> prettier way to achieve the same result.

I debated myself about which one to use in the example. The dunder
aliases were added for completeness, to clarify the relationship to
the corresponding special method, and I suppose to make it simpler to
dynamically call them. I opted to use the explicit name, even though
it's an alias because it seemed to make the example more consistent.
Normally I would call operator.add.


More information about the Python-list mailing list