Reposting because the original got bounced from Google Groups.

---------- Forwarded message ---------
From: Tim Peters <tim.pet...@gmail.com>
Date: Fri, Jun 29, 2018 at 5:54 PM
Subject: Re: [Python-ideas] collections.Counter should implement fromkeys
To: <abedil...@gmail.com>
Cc: python-ideas <python-id...@googlegroups.com>


[Abe Dillon <abedil...@gmail.com>]
> ...
> 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>.

More because Counter.fromkeys() could be incoherent.  From the
implementation (in your Lib/collections/__init__.py):

    @classmethod
    def fromkeys(cls, iterable, v=None):
        # There is no equivalent method for counters because setting v=1
        # means that no element can have a count greater than one.
        raise NotImplementedError(
            'Counter.fromkeys() is undefined.  Use Counter(iterable)
instead.')

For a dict, a value appearing multiple times in the iterable doesn't
matter.  But a fundamental use case for Counters is to tally the _number_
of times duplicate keys appear.  So, e.g., someone will be unpleasantly
surprised no matter what:

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!" "You're both right - and
wrong!  It should raise an exception if there's a duplicate key, because
there's no compelling answer to what it should do!"

I expect Raymond called it NotImplementedError instead so he could release
the code instead of waiting 3 years for that debate to end ;-)
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to