set.add could return True or False
14 Mar
2012
14 Mar
'12
12:36 p.m.
set.add(x) could return True if x was added to the set, and False if x was already in the set. Adding an element that is already present often constitutes an error in my code. As I understand, set.add is an atomic operation. Having set.add return a boolean will also allow EAFP-style code with regard to handling duplicates, the long winded form of which is currently: if a not in b: b.add(a) <-- race condition do_c() Which can be improved to: if b.add(a): do_c() Advantages: * Very common code pattern. * More concise. * Allows interpreter atomicity to be exploited, often removing the need for additional locking. * Faster because it avoids double contain check, and can avoid locking.
4395
Age (days ago)
4397
Last active (days ago)
13 comments
8 participants
participants (8)
-
Andrew Svetlov
-
Cameron Simpson
-
Chris Rebert
-
Guido van Rossum
-
Jacob Holm
-
Masklinn
-
Matt Joiner
-
Raymond Hettinger