[Python-ideas] Fwd: collections.Counter should implement fromkeys

Abe Dillon abedillon at gmail.com
Fri Jun 29 20:32:54 EDT 2018

[Tim Peters] 

> a fundamental use case for Counters is to tally the _number_ of times 
> duplicate keys appear.
Yes, that's why the default constructor already does just that. 

[Tim Peters] 

> So, e.g., someone will be unpleasantly surprised no matter what

Sure, but in Hettinger's own words 
<https://www.youtube.com/watch?v=HTLu2DFOdTg&t=24m46s> "whenever you have a 
constructor war, everyone should get their wish". People that want a 
counting constructor have that,
people that want the ability to initialize values don't have that.

[Tim Peters] 

> Counter.fromkeys("aaaaa", 2)
> returned.  "It should set key 'a' to 2!  that's what I said it should 
> do!"  "No!  It should set key 'a' to 10!  that's what a Counter _always_ 
> does - sums the values associated with duplicate keys!"

I'm tempted to indulge in the meta argument which you're obviously striving 
to avoid, but I will say this:
"that's what a Counter _always_ does" makes no sense. It's *almost* 
tantamount to saying that all constructors have to do exactly the same 
thing, which makes multiple constructors useless.

Technically, there is no constructor for counting by X, but if enough 
people really wanted that, I suppose a third constructor would be in order.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180629/3fc921cc/attachment.html>

More information about the Python-ideas mailing list