[issue22533] Counter with no keys does not compare equal to Counter with keys which zero value

2020-05-23 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
pull_requests: +19607
pull_request: https://github.com/python/cpython/pull/20339

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22533] Counter with no keys does not compare equal to Counter with keys which zero value

2014-10-11 Thread Raymond Hettinger

Raymond Hettinger added the comment:

> It is my thought that a Counter with all its keys set to zero 
> is as empty as a Counter with no keys

If the counter were a stand-alone multiset or bag class, this might be a 
reasonable thing to do.

However, for better or for worse, the design of the counter is primarily "a 
dict with a __missing__ that returns 0 and adds some handy methods useful in 
the context of dicts being used for counting".   This gives it flexibility, 
simplicity, speed, and makes it more or less substitutable for regular dicts in 
functions that expect regular dicts.  IIRC, this is the basic design that Guido 
endorsed when the idea of a counter was first proposed.

> I agree it's sort of weird, but I feel like fixing it will 
> just break a ton of existing code.

It is sort of weird, but that was the intended behavior (trying to keep the 
counter as dict-like as possible), and you're correct that changing it now 
would risk either breaking or substantially slowing code existing code.

--
resolution:  -> not a bug
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22533] Counter with no keys does not compare equal to Counter with keys which zero value

2014-10-10 Thread Raymond Hettinger

Changes by Raymond Hettinger :


--
assignee:  -> rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22533] Counter with no keys does not compare equal to Counter with keys which zero value

2014-10-02 Thread Ethan Furman

Ethan Furman added the comment:

Ignore that last comment -- I don't know what I did yesterday, but unary minus 
is working as expected today.  :/

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22533] Counter with no keys does not compare equal to Counter with keys which zero value

2014-10-01 Thread Ethan Furman

Ethan Furman added the comment:

Exactly what operation is unary minus supposed to be?  It seems to act like 
absolute value.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22533] Counter with no keys does not compare equal to Counter with keys which zero value

2014-10-01 Thread Ned Deily

Changes by Ned Deily :


--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22533] Counter with no keys does not compare equal to Counter with keys which zero value

2014-10-01 Thread Josh Rosenberg

Josh Rosenberg added the comment:

Reading the note on the Counter class (about intent vs. actual use), it looks 
like changing this behavior would involve potentially breaking a lot of code.

If you're using Counters that are intended to maintain positive counts 
(treating a count <= 0 as if the key does not exist), it seems like you could 
either perform your operations using the + and - (or += and -=) binary 
operators (which will remove keys when the values drop to 0 or below) or if you 
have to use subtract or manual count tweaking, normalize the Counters at 
comparison time, that is, use +counter1 == +counter2 (unary + support added in 
3.3).

I agree it's sort of weird, but I feel like fixing it will just break a ton of 
existing code.

--
nosy: +josh.rosenberg

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22533] Counter with no keys does not compare equal to Counter with keys which zero value

2014-10-01 Thread Ethan Furman

New submission from Ethan Furman:

According to the docs [1]:

> Counter objects have a dictionary interface except that they return a
> zero count for missing items instead of raising a KeyError

Which a simple test confirms:

--> Counter()['b']
0

However, if the key is present but set to zero, equality fails:

--> Counter() == Counter(b=0)
False

It is my thought that a Counter with all its keys set to zero is as empty as a 
Counter with no keys:

--> c1 = Counter()
--> c2 = Counter(a=0, b=0, c=0)
--> for item in c2.keys():
...   assert c2[item] == c1[item]
(no execption raised)


[1] https://docs.python.org/2/library/collections.html#collections.Counter

--
messages: 228111
nosy: ethan.furman
priority: normal
severity: normal
status: open
title: Counter with no keys does not compare equal to Counter with keys which 
zero value
type: behavior
versions: Python 2.7, Python 3.4, Python 3.5

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com