On Thu, Jun 25, 2020 at 6:44 PM Alex Hall <alex.mojaki@gmail.com> wrote:

The latest of these discussions is over 8 years old, and I think it deserves bringing up again.

The convention that "methods that modify self return None" is useful, but I would say that dict's equivalent method: dict.setdefault is a modifying method that also returns a pertinent (and often useful value), even though it could too be split into two methods (__getitem__ and __setitem__ if the key is missing), as does dict.pop (BTW, I think that set.discard should also return a boolean, but that discussion can wait).

Indeed, adding a return value to set.add would not improve runtime performance, but I believe it would both improve readability and make code more fluent to write.

As for teaching, I don't think this additional bit of complexity would be too difficult to learn. "set.add ensures that an item is in the set and returns whether it was already included" seems pretty straightforward to me. Not to mention that anyone coming from C#, Java, C++, and many other languages would already be familiar with this concept.

Overall, I think my gripe is that, currently, set() does not have the capabilities of a dict that maps to NoneType, that the simplest implementation of set in python is more powerful than the standard library's version.