
FWIW, you can do it with dict already.
o = memo.setdefault(o, o)
I don't think this has quite the same behavior as the OP is looking for, since dict.setdefault() will insert the key and return the default when it's not present, instead the OP wanted to raise "KeyError if the object is not present". In order to raise a KeyError from a missing key and have the values be the same as the keys, one could build a dictionary like this: ``` d = {} for item in sequence: d[item] = item ``` or using comprehension: ``` d = {item: item for item in sequence} ``` and then ``` try: val = d['foo'] except KeyError: ... ``` But yeah, this behavior already exists for dictionaries. Personally, I think some_set['foo'] would likely: 1) Not make much sense for usage in sets, from a design perspective. 2) Lack practical value, as opposed to simply using a dictionary for the same purpose. To me, this feature doesn't seem worthwhile to implement or maintain. There are probably other reasons to consider as well. However, if the OP wants to personally implement this behavior for their own subclass of sets (instead of adding it to the language), that could be done rather easily: ```
class MySet(set): ... def __getitem__(self, key): ... if key not in self: ... raise KeyError(f'{key} not present in set') ... else: ... return key ... s = MyClass({'a', 'b', 'c'}) s['a'] 'a' s['d'] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 4, in __getitem__ KeyError: 'd' not present in set
This may not have the same performance as d[key] since it has the
conditional check for membership, but it provides the same functionality.
In 90% of use cases the performance difference should be very negligible. I
don't think that I'd advocate for using the above instead of a dict, but
it's rather straightforward to implement if desired.
On Mon, Dec 2, 2019 at 7:39 PM Inada Naoki <songofacandy@gmail.com> wrote:
> FWIW, you can do it with dict already.
>
> o = memo.setdefault(o, o)
>
> On Tue, Dec 3, 2019 at 9:29 AM Soni L. <fakedme+py@gmail.com> wrote:
> >
> > This is an odd request but it'd be nice if, given a set s = {"foo"},
> > s["foo"] returned the "foo" object that is actually in the set, or
> > KeyError if the object is not present.
> >
> > Even use-cases where you have different objects whose differences are
> > ignored for __eq__ and __hash__ and you want to grab the one from the
> > set ignoring their differences would benefit from this.
> > _______________________________________________
> > 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/T3Z32DEMWK46EBPULYB4CVI2QF4FS3WJ/
> > Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
> --
> Inada Naoki <songofacandy@gmail.com>
> _______________________________________________
> 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/Q2EPKWPSVG55A3CKVCLJGRX6SPKKSIEE/
> Code of Conduct: http://python.org/psf/codeofconduct/
>