Minus operator versus unary minus

Peter Otten __peter__ at web.de
Sat May 30 04:36:48 EDT 2015


Steven D'Aprano wrote:

> 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'

Hm, do you have an example of a Counter (or subclass) instance where the 
values are not int?




More information about the Python-list mailing list