operator module functions
rosuav at gmail.com
Wed Oct 8 11:32:11 CEST 2014
(You didn't include any context in your post. Please quote as much
text as would be helpful; it's the easiest way to show what you're
On Wed, Oct 8, 2014 at 7:46 PM, <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.
What Steven's talking about is this:
>>> operator.add is operator.__add__
It's the exact same function, just accessed with a different name.
> As an example, you cannot use the dunder versions for literals.
>>> 2.__add__(3) # Oops, does not work
>>> a = 2
That's actually just a syntactic issue with integers and the dot. It
works fine if you use any other form of literal, or if you put a space
between the digits and the dot, or use parentheses, or anything; this
is the case with all methods off integers, not just dunder ones.
>>> 2 .__add__(3)
The object is exactly the same whether you reference the literal '2'
or the name 'a' that you've bound to it, so its methods must by
definition all be there.
More information about the Python-list