Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91517

Re: Minus operator versus unary minus

From Peter Otten <__peter__@web.de>
Subject Re: Minus operator versus unary minus
Date 2015-05-30 10:36 +0200
Organization None
References <mailman.215.1432972613.5151.python-list@python.org> <55697284$0$12992$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.216.1432975020.5151.python-list@python.org> (permalink)

Show all headers | View raw


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?

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Minus operator versus unary minus Peter Otten <__peter__@web.de> - 2015-05-30 09:56 +0200
  Re: Minus operator versus unary minus Steven D'Aprano <steve@pearwood.info> - 2015-05-30 18:19 +1000
    Re: Minus operator versus unary minus Peter Otten <__peter__@web.de> - 2015-05-30 10:36 +0200

csiph-web