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

Steven D'Aprano steve at pearwood.info
Mon Apr 22 20:19:30 EDT 2019


On Mon, Apr 22, 2019 at 10:06:20AM -0700, Chris Barker via Python-Dev wrote:

maybe a new dict mapping type -- "shareddict" -- it would be used in places like the csv reader where it makes sense, but wouldn't impact the regular dict at all.

you could get really clever an have it auto-convert to a regular dict when any changes were made that are incompatible with the shared keys...

Oh, you mean just like regular dicts with shared keys already do :-)

https://www.python.org/dev/peps/pep-0412/

Perhaps I've missed something in this discussion, but isn't this a matter of just making the existing shared-keys functionality explicitly usable rather than just purely implicit? Quoting from the PEP:

When dictionaries are created to fill the dict slot of an object, they are created in split form. The keys table is cached in the type, potentially allowing all attribute dictionaries of instances of one class to share keys. In the event of the keys of these dictionaries starting to diverge, individual dictionaries will lazily convert to the combined-table form.

There's no explicit interface to control this; it all happens by magic, behind the scenes. I think the proposal here is to add some sort of interface, possibly a new method, to explicitly use key sharing.

-- Steven



More information about the Python-Dev mailing list