Stephen Webber added the comment:
Hmm, that is odd behavior indeed.
I think having keys that point to zero values is important for iterating over a
set. For example:
x = Counter(a=10, b=0)
for k in set(x):
... x[k] += 1
...
x
Counter({'a': 11, 'b': 1})
is probably preferable to
x =
Raymond Hettinger added the comment:
At its most basic, a Counter is simply a dictionary with a __missing__ method
that supplies a default of zero. It is intentional that everything else
behaves as much like a regular dictionary as possible. You're allowed to store
*anything* in the dict
Eric Snow added the comment:
I'd missed that unary + (new in 3.3). That's pretty cool.
--
nosy: +eric.snow
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue14182
___
Mark Dickinson added the comment:
Raymond, Stephen's analysis seems correct. Are we missing something or
can this issue be closed?
Well, depending on how you think about Counters, the current behaviour of
equality definitely leads to some surprises. For example:
Counter(a = 3) +
Meador Inge added the comment:
Ah, good examples Mark. So, why is it ever useful keep a key with a value of
zero? In other words, why:
Counter(a=0)
Counter({'a': 0})
instead of:
Counter(a=0)
Counter()
?
The latter seems more consistent to me.
--
Meador Inge added the comment:
Raymond, Stephen's analysis seems correct. Are we missing something or can
this issue be closed?
--
nosy: +meador.inge
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue14182
Stephen Webber added the comment:
This is intentional handling of non-existant variables, and is not resticted to
'==' operations. Returning the value of a Counter parameter that has not yet
been set returns 0 by default.
See the documentation here:
New submission from Raymond Hettinger raymond.hettin...@gmail.com:
from collections import Counter
x=Counter(a=10,b=0,c=3)
y=Counter(a=10,c=3)
x == y
False
all(x[k]==y[k] for k in set(x) | set(y))
True
--
assignee: rhettinger
components: Library (Lib)
messages: 154827
nosy: