[Python-ideas] Add dict.append and dict.extend

Chris Barker chris.barker at noaa.gov
Wed Jun 6 02:18:34 EDT 2018


On Tue, Jun 5, 2018 at 4:10 PM, Steven D'Aprano <steve at pearwood.info> 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
> Python-ideas at python.org
> 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

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180605/12423e0d/attachment.html>


More information about the Python-ideas mailing list