I just tried playing with this idea:
from collections import UserDict
class InsertOrIgnoreDict(UserDict):
__setitem__ = UserDict.setdefault
print(InsertOrIgnoreDict([(1, 2), (3, 4)]))
It caused an infinite chain of exceptions:
Traceback (most recent call last):
File "/home/alex/.pyenv/versions/3.8.0/lib/python3.8/_collections_abc.py", line 845, in setdefault
return self[key]
File "/home/alex/.pyenv/versions/3.8.0/lib/python3.8/collections/__init__.py", line 1003, in __getitem__
raise KeyError(key)
KeyError: 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/alex/.pyenv/versions/3.8.0/lib/python3.8/_collections_abc.py", line 845, in setdefault
return self[key]
File "/home/alex/.pyenv/versions/3.8.0/lib/python3.8/collections/__init__.py", line 1003, in __getitem__
raise KeyError(key)
KeyError: 1
During handling of the above exception, another exception occurred:
...
I thought that was amusing.
Anyway, one could come up with infinitely many variations of potentially useful semantics. Why include any of them in the language instead of letting users implement what they need?
On Thu, Apr 16, 2020 at 2:37 PM Soni L. <fakedme+py@gmail.com> wrote:
currently dicts have insert-or-update semantics, e.g.:
>>> dict([((), 1), ((), 2)])
{(): 2}
this is fine. however, in many cases insert or insert-or-ignore are more
useful: (hypothetical examples)
>>> InsertOrIgnoreDict([((), 1), ((), 2)])
{(): 1}
>>> InsertDict([((), 1), ((), 2)])
Traceback: [...]
ValueError: key exists
these are useful when dealing with custom config file formats, config
overrides, among other things.
additionally we could also get an UpdateDict, a *view* into a dict that
only allows updating existing entries:
>>> a = {'a': 1}
>>> b = {'a': 2, 'b': 3}
>>> UpdateDict(a).update(b)
>>> a
{'a': 2}
but I don't see an obvious usefulness to this one. it'd just be for
consistency with other SQL-esque dict wrappers (like the above
InsertOrIgnoreDict and InsertDict).
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/7PCWDORFLBFY6HRLNNS6UBL2CRER26SM/
Code of Conduct: http://python.org/psf/codeofconduct/