Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dict.update() to take dict, add update_items() to take iterable? #2041

Open
plajjan opened this issue Dec 23, 2024 · 3 comments
Open

dict.update() to take dict, add update_items() to take iterable? #2041

plajjan opened this issue Dec 23, 2024 · 3 comments

Comments

@plajjan
Copy link
Contributor

plajjan commented Dec 23, 2024

dict.update() takes an iterable:

protocol Mapping[A(Eq),B] (Container[A], Indexed[A,B]):
    update      : mut(Iterable[(A,B)]) -> None

this is weird because we write:

d = {}
d.update({"foo": "bar"}.items())

it would be more natural to not have to do .items(), like so:

d = {}
d.update({"foo": "bar"})

should we change update to take a Mapping instead? We know Mapping supports items.

We can add a update_items() method that acts as the current .update() in case someone has a different source of items to update.

@sydow @nordlander WDYT?

@sydow
Copy link
Collaborator

sydow commented Dec 23, 2024 via email

@nordlander
Copy link
Contributor

Isn't the core problem that we've followed Python's view that iteration over a dict means iterating over its keys only? Instead, if Mapping[A,B] would inherit Container[(A,B)] and not Container[A], the update ugliness would go away I think.

The downside would of course be that x in some_dict then must mean checking whether some key-value pair is in some_dict. Perhaps not that useful, but maybe one could find a way to keep the Python meaning in this particular case...

@nordlander
Copy link
Contributor

Oop, overlapping with Björn's comment. Sorry...!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants