Dan Sommers writes:
Perhaps by your standard [it's not EAFP]. The code I wrote[:]
already_there = seen.add(element) if already_there: # handle the duplicate case
performs an operation, and then asks whether or not some condition was met, as opposed to asking whether the condition is met first, and then conditionally performing the operation.
Aside: Checking status returns seems like LBYL to me, too. EAFP doesn't ask at all, it responds to a tug on its sleeve. :-)
I think the point that we could cheaply have a (nearly?) atomic "check_presence_and_add_if_not" operation is valid. I can see how that might be useful in coordinating resource access, for example.
Given dict.setdefault, a parallel method like set.add_unique, which returns the existing element or None, might be the better approach. This would also allow the programmer to check for (2 is not 2.0) if he wants to.