git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Proposal: dict.with_values(iterable)


On Sat, Apr 13, 2019 at 12:38 AM Serhiy Storchaka <storchaka at gmail.com> wrote:
>
> It looks contrary to simplification made in Python 3 when we get rid of
> some more efficient lists in favor of more general iterators.
>

Yes.  This is API for special use case creates many dict having
same keys, like csv.DictReader.
It is not good design for general purpose.

strings module has strings.Template class.  But I don't want to add
dicts module.
Maybe, collections.DictBuilder may be another option.  e.g.

>>> from collections import DictBuilder
>>> builder = DictBuilder(tuple("abc"))
>>> builder.build(range(3))
{"a": 0, "b": 1, "c": 2}


> If this is a common case we can add an invisible optimization for
> dict(zip(keys, values)), especially if keys is a key-sharing dictionary.
> This will benefit all users without the need to rewrite the code to use
> the new special method.

But this optimization may slow down when creating one dict...

>
> The interface of dict is already overloaded. It contains many methods
> which most users use rarely (and therefore which are not kept in the
> working set of memory).

Yes.

-- 
Inada Naoki  <songofacandy at gmail.com>