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

Bill Janssen janssen at parc.com
Mon Feb 16 20:47:07 CET 2009


Steven D'Aprano <steve at pearwood.info> wrote:

> 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:
>     myset.add(42)

Well, for me, What's wrong is that it's complex to write and debug,
mainly.  Don't you mean to say,

     was_it_there = (42 in myset)
     if not was_it_there:
         myset.add(42)

for instance?  Not that I'm pushing for the addition of this method, but
I see the point.

Bill



More information about the Python-ideas mailing list