[Python-ideas] Fwd: Re: PEP: add a `no` keyword as an alias for `not`
Andrew Barnert
abarnert at yahoo.com
Fri Aug 2 12:17:39 EDT 2019
On Aug 1, 2019, at 13:38, Daniel Okey-Okoro <danielokeyokoro at gmail.com> wrote:
>
>
> > not a strong enough justification for breaking any code that uses "no" in any other way.
>
> This is a very crucial point I didn't consider.
>
> ------------------------------------------------------------
>
> What if we could lexically limit it to `if no {{everything else in the if block}}`?
>
> I think that would sufficiently protect us from unintentionally breaking people's code.
There are some cases where a “contextual keyword” that’s usable as an identifier outside of a specific syntactic construct could avoid ambiguity, but this isn’t one of them, because the thing immediately after the `if` in an if statement can be–and often is—an identifier. For example:
total = len(votes)
yes, no, other = (len(part) for part in partition_votes(votes))
if no >= total//2:
# etc.
I even found an actual example of the equivalent in some C++ code I had on my hard drive:
if (no == ask(…)) {
// …
}
In Python, that would be:
if no == ask(…):
Also, even if that weren’t a problem, this would be very misleading syntax. If I can write `if no sales:` I’d expect to be able to write `if isopen and no sales:` or `0 if no sales else mean(sales)` or `skip = no sales` or any of the other things I can do with `not` and other operators rather than special syntax.
Also, I could still write `if no sales and isopen:`, but it would do the wrong thing if `no` is special syntax that reverses the sense of the `if` rather than a normal operator that binds more tightly than `and`.
More information about the Python-list
mailing list