On Fri, Jun 26, 2020 at 7:58 PM Steven D'Aprano <st...@pearwood.info> wrote: > Most importantly, it matches the way people think about the task: > > # Task: look for duplicates > if element in seen: > # it's a duplicate > ... > else: > # never seen before, so remember it > ...
Do you do this: if file exists: # read the file else: # create the file Or would you try/except around an open call? The TOCTOU issue may be less obvious with sets, but it's still a reasonable thing to concern yourself with. > This idiom works with sets, it works with lists, it works with trees, it > works with anything that supports membership testing and inserting into > a collection. It is the natural way to think about the task. So would "ensure this is here, and let me know if you created it". Or "add this, and throw an exception if it was already there", which comes to the same thing, but uses exception handling as its mechanism. > Now, I agree that sometimes for the sake of efficiency, we need to do > things in a weird way. But membership testing in sets is cheap, it's > not a linear search of a huge list. So the efficiency argument here is > weak. TOCTOU is stronger. > But either way, you also have to decide whether the `add` (or the new > method) should *unconditionally* insert the element, or only do so if it > wasn't present. This makes a big difference: > > seen = {2} > already_there = seen.add(2.0) > > > At this point, is `seen` the set {2} or {2.0}? Justify why one answer is > the best answer. > It should do the same thing that happens already: keep the existing one. I don't see why this is even in question. Are you searching that hard for objections? ChrisA _______________________________________________ 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/G2RRY7BJLFROU7DMTOWL44X455254RBC/ Code of Conduct: http://python.org/psf/codeofconduct/