[Python-ideas] collections.Counter should implement fromkeys

Abe Dillon abedillon at gmail.com
Fri Jun 29 18:19:12 EDT 2018


[Michael Selik] 

> You need an iterable of the keys you're interested in to pass to the 
> hypothetical ``fromkeys`` method. Why not iterate over that same iterable 
> paired with zeros instead of passing it into ``fromkeys``? 
>

Because, as in my original example code; the values could be zero or they 
could be more, I just want to make sure the keys are in the counter when I 
iterate.
I'm not having any trouble finding a work around. I'm having trouble 
understanding why I need to find a work around when Counter already 
inherits from dict, dict.fromkeys is perfectly well defined, and there's 
not really any other *obvious* best way to initialize the value for a set 
of keys.

Counter(dict.fromkeys(keys, value)) works just fine, but it feels wrong.

I'm using the copy-constructor because I know Counter is a subclass of dict.
I'm using fromkeys because I know how that class method works.
So why does the subclass lack functionality that the superclass has?
Because programmers wouldn't be able to wrap their heads around it?
I don't buy it. This feels like nanny-design trumping SOLID design 
<https://en.wikipedia.org/wiki/SOLID>.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180629/3b5d6ab8/attachment.html>


More information about the Python-ideas mailing list