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


Groups > comp.lang.python > #91515 > unrolled thread

Minus operator versus unary minus

Started byPeter Otten <__peter__@web.de>
First post2015-05-30 09:56 +0200
Last post2015-05-30 10:36 +0200
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#91515 — Minus operator versus unary minus

FromPeter Otten <__peter__@web.de>
Date2015-05-30 09:56 +0200
SubjectMinus operator versus unary minus
Message-ID<mailman.215.1432972613.5151.python-list@python.org>
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

?

[toc] | [next] | [standalone]


#91516

FromSteven D'Aprano <steve@pearwood.info>
Date2015-05-30 18:19 +1000
Message-ID<55697284$0$12992$c3e8da3$5496439d@news.astraweb.com>
In reply to#91515
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

[toc] | [prev] | [next] | [standalone]


#91517

FromPeter Otten <__peter__@web.de>
Date2015-05-30 10:36 +0200
Message-ID<mailman.216.1432975020.5151.python-list@python.org>
In reply to#91516
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?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web