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

Sergey Fedoseev fedoseev.sergey at gmail.com
Fri Sep 8 12:24:20 EDT 2017


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.


More information about the Python-ideas mailing list