This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make set's add and discard methods return useful information
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: brechtm, r.david.murray, rhettinger
Priority: normal Keywords:

Created on 2014-11-15 22:47 by brechtm, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg231226 - (view) Author: Brecht Machiels (brechtm) Date: 2014-11-15 22:47
set's add() method would be a little bit more useful if it would return True if the added value was already present in the set, and False if it wasn't (or the other way around). Similarly, discard() could report whether the discarded value was present in the set or not.

I suppose this could cost a couple of extra cycles and I'm not sure this is acceptable. For discard() there's always remove() that offers similar behavior. add() does not have an alternative that offers check-and-add behavior.
msg231227 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-11-15 23:11
Hmm.  The fact that it could be either way around makes it less attractive.  That is, if it isn't intuitively obvious what the truth value would mean, it is probably a bad idea to have this behavior.
msg231229 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-11-16 00:53
This has come up once before and it was rejected for several reasons including the one David mentioned.

In Python, code reads more clearly with the usual:

   for elem in iterable:
       if elem not in seen:
           seen.add(elem)
           do_something(elem)

Than with:

   for elem in iterable:
       if not seen.add(elem):
           do_something(elem)


That latter is less self-evident about what it does.

Also, I think there were lessons drawn from Guido's decision to not incorporate the C-language feature of both assigning and testing in a conditional:  

    while((c = fgetc(fp)) != EOF)
History
Date User Action Args
2022-04-11 14:58:10adminsetgithub: 67068
2014-11-16 00:53:37rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg231229
2014-11-15 23:11:06r.david.murraysetnosy: + rhettinger, r.david.murray
messages: + msg231227
2014-11-15 22:47:49brechtmcreate