>
> Given dict.setdefault, a parallel method like set.add_unique, which
> returns the existing element or None, might be the better approach.
>

Although it breaks the convention of these methods returning the item, I'm
not sure returning the element is a good idea.
"set.add_unique(None)"  will return None.

The original example of "if dict.setdefault()" is problematic as well for
the exact same reason. You can't tell if the default value was already
there in the first place.

@Chris
The set.add is already threadsafe as GIL is not checked while running the
function, but it shouldn't be relied upon, if only because of ABC not
guaranteeing other implementations to be threadsafe, let alone other python
implementations as well.

De-facto I've never seen TOCTOU issues with these things as they don't
contain external resource access and are usually surrounded by locks for
plenty of other reasons, but theoretically it can be useful to prevent the
need of locks.

Either way, modifying the existing method is probably a no-no - it will
break backwards compatibility in both Python and C-API.

Regarding a new method, what are the actual arguments in favor? Apart from
saving one line of code ofc. Besides, I don't believe trying to 1-line it
makes the code any clearer, but that's personal taste.

>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/WQDGF25GDFID73F4FKCJAGBH6T4ETGQX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to