operator module functions

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Oct 8 12:57:40 CEST 2014


marco.nawijn at colosso.nl wrote:

> For me it makes sense. operator.add should be used in a "global" context
> (I don't know how to express it otherwise). So you provide it with the
> two values that you want to add. The .__add__ variants are bound to a
> particular instance and you provide it with a single value that you want
> to add.

That's not correct. You're misunderstanding me, I'm not talking about the
special "dunder" methods on classes, e.g. (2).__add__(3) -> 5. I'm talking
about *functions* in the operator module:

py> import operator
py> operator.add(2, 3)
5
py> operator.__add__(2, 3)
5

According to the documentation, operator.__add__ is the "official" function,
and operator.add is just there for convenience. Neither of them is the
special method __add__. But it seems that nobody uses the official
operator.__add__ function, and Terry Reedy says there are no tests for it.



-- 
Steven




More information about the Python-list mailing list