[Python-ideas] factory for efficient creation of many dicts with the same keys

INADA Naoki songofacandy at gmail.com
Fri Sep 8 14:16:42 EDT 2017


Thanks for your suggestion.

FYI, you can use "key-sharing dict" (PEP 412:
https://www.python.org/dev/peps/pep-0412/)
when all keys are string.
It saves not only creation time, but also memory usage.
I think it's nice for CSV parser and, as you said, DB record.

One question is, how is it useful?
When working on large dataset, I think list or tuple (or namedtuple)
are recommended for records.

If it's useful enough, it's worth enough to added in dict.
It can't be implemented as 3rd party because relying on many private in dict.

Regards,
INADA Naoki  <songofacandy at gmail.com>


On Sat, Sep 9, 2017 at 1:24 AM, Sergey Fedoseev
<fedoseev.sergey at gmail.com> wrote:
> Here's docs:
>
>    .. staticmethod:: factory(*keys)
>
>       Return a callable object that creates a dictionary from *keys* and its
>       operands.  For example:
>
>       * ``dict.factory('1', 2, (3,))({1}, [2], {3: None})`` returns
>         ``{'1': {1}, 2: [2], (3,): {3: None}}``.
>
>       * ``dict.factory((3,), '1', 2)({1}, [2], {3: None})`` returns
>         ``{(3,): {1}, '1': [2], 2: {3: None}}``.
>
>       Equivalent to::
>
>          def factory(*keys):
>              def f(*values):
>                  return dict(zip(keys, values))
>              return f
>
> Hope it makes my idea clearer.
>
> Link to patch (I guess it's too big to paste it here):
> https://github.com/sir-sigurd/cpython/commit/a0fe1a80f6e192368180a32e849771c420aa0adc
>
> 2017-09-08 19:56 GMT+05:00 Guido van Rossum <guido at python.org>:
>> I think you've got it backwards -- if you send the patch the idea *may* be
>> accepted. You ought to at least show us the docs for your proposed factory,
>> it's a little murky from your example.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list