On Tue, Jun 5, 2018 at 4:10 PM, Steven D'Aprano <[email protected]> wrote:
> I'm confused... first you say that Ben makes a good case for this
> functionality with the DB analogy, and then one sentence later, you say
> the DB case is very different. So not a good case? I don't understand.
>
I wasn't trying to make a case either way -- on the one hand, there is a
good analogy to DB UNIDQUE, on the other hand, dicts are really pretty
different than DBs.
> And what is this way of spelling "it" (what is it?) that's
> straightforward and robust? You've completely lost me, sorry.
>
if key in dict:
raise KeyError
if you had to do that with a DB before adding a record, it could be a
pretty expensive operation....
Thinking on this a bit more, I'm pretty -1 -- the main reason that if we
had a
dict.exclusive_add() method, when you wanted to use it, you'd have to catch
the KeyError and do something:
try:
my_dict.exclusive_add(key, val)
except KeyError:
do_something_else_probably_with(my_dict, val)
since you'd have to catch it, then you aren't really simplifying the code
much anyway:
if key in my_dict:
so_something_else
else:
my_dict[key] = val
the same amount of code and I think the explicit check is clearer....
Also -- seems kind of odd to raise a KeyError when the key IS there?!?
-CHB
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
[email protected]
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/