Minus operator versus unary minus

Steven D'Aprano steve at pearwood.info
Sat May 30 04:19:16 EDT 2015


On Sat, 30 May 2015 05:56 pm, Peter Otten wrote:

> The following modification of the collections.Counter implementation
> 
> https://hg.python.org/cpython/rev/fe4efc0032b5
> 
> was just checked in with the line
> 
> result[elem] = 0 - count
> 
> Does this have an advantage over the obvious
> 
> result[elem] = -count
> 
> ?


py> class Thingy:
...     def __neg__(self):
...         return [1, 2, 3, 4]
...
py> -Thingy()
[1, 2, 3, 4]
py> 0 - Thingy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'int' and 'Thingy'




-- 
Steven




More information about the Python-list mailing list