Bar Harel writes:
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.
That's not a problem. In both cases if you need an object that can't possibly be there, you have
# 'default' by analogy to dict.setdefault. It's a bad name for # this argument to add_unique, and does not have the semantics of # inserting default in the set.
olditem = someset.add_unique(thing, default=(sentinel:=object())) if olditem is not sentinel: # handle dupe
More likely you trust yourself not to insert it, and use a sentinel defined previously.
Of course, we may prefer a boolean return, despite the general rule about returning elements. I'm single-threaded, and so agnostic on that. :-) But if it turns out that somebody *wants* to check "2 is 2.0", this .add_unique can serve both purposes.