[Python-ideas] an unless statement would occasionally be useful

Andrew Barnert abarnert at yahoo.com
Sun May 17 23:35:01 CEST 2015


On May 17, 2015, at 11:07, Charles Hixson <charleshixsn at earthlink.net> wrote:
> 
> I'm envisioning "unless" as a synonym for "if not(...):"  currently I use
> 
> if .... :
>    pass
> else:
>  ...
> 
> which works.
> 
> N.B.:  This isn't extremely important as there are already two ways to accomplish the same purpose, but it would be useful, seems easy to implement, and is already used by many other languages.  The advantage is that when the condition is long it simplifies understanding.

But if you just use not instead of else, it simplifies understanding just as much--and without making the language larger (which makes it harder to learn/remember when switching languages, makes the parser bigger, etc.):

    if not ...:
        ...

It seems like every year someone proposes either "unless" or "until" or the whole suite of Perl variants (inherently-negated keywords, postfix, do...while-type syntax), but nobody ever asks for anything clever. Think of what you could do with a "lest" statement, which will speculatively execute the body and then test the condition before deciding whether to actually have executed the body. Or a "without" that closes a context before the body instead of after. Or a "butfor" that iterates over every extant object that isn't contained in the Iterable. Or a "because" that raises instead of skipping the body if the condition isn't truthy. Or a "before" that remembers the body for later and executes it a synchronously when the condition becomes true.



> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list