[Python-ideas] set.add() return value

Steven D'Aprano steve at pearwood.info
Sat Feb 14 02:24:41 CET 2009

Greg Ewing wrote:
> Chris Rebert wrote:
>> As The Zen says: "Special cases aren't special enough to break the 
>> rules."
> What might be more acceptable is to add a new method
> for this, with a name suggesting that it's more than
> just a plain mutating operation, e.g.
>   was_it_there = myset.test_and_add(42)

What's the use-case for this? What's wrong with doing this?

if 42 in myset:

Short, sweet, and doesn't require any new methods.

The OP's original use-case was based on his misapprehension that key 
lookup in a set was O(N log N). I don't see any useful advantage to a 
new method, let alone a change in semantics to the existing method.


More information about the Python-ideas mailing list