[Python-ideas] "while:" for the loop
Stephen J. Turnbull
turnbull.stephen.fw at u.tsukuba.ac.jp
Thu Sep 27 02:00:15 EDT 2018
Mikhail V writes:
> In the first linked discussion, e.g. Mr. D'Aprano and Mr. Coghlan (from my
> impression both from dev team) unambiguosly claimed that adding e.g.
> "loop" as a loop token lead to necessity of excluding ALL
> variables and functions/methods named "loop" from all sources.
AIUI, the situation is this:
The restriction was imposed by Guido as a design feature.
There are three general possibilities:
1. The syntax recognizes some tokens as significant, but they are not
reserved at all. It's arguable that Lisp works this way (some
will argue that only parentheses create syntax in Lisp, but I don't
think humans perceive it that way).
2. The syntax recognizes keywords (ie, reserved for their syntactic
meaning) in a context-dependent fashion. In Python, an example
might be treating break and continue this way: inside a loop they
are reserved, but you can use them as identifiers outside of a
loop. I believe that the async-related keywords were treated this
way at first.
3. Syntactically-significant keywords are reserved for their
syntactic purpose globally. This is the rule in Python.
Guido chose #3 as a matter of designing Python. IIRC, the logic was
that #1 admits awful unreadable code like "if if then then else else"
and nobody needs to write that, and it's hard to write a grammar for
it that allows generation of good error messages. The rules for #2,
being context-dependent, are unclear to most humans and you don't get
a significant benefit for many keywords (consider how useful "break"
would be as a variable if you couldn't reference it in a loop!)
Besides not having those disadvantages, #3 has the advantage of a
simpler grammar and more transparent syntax error reporting, at the
minor cost of reserving a very few tokens globally.
This does get annoying sometimes (you see a lot of use of 'class_' and
'klass' in some contexts), but I hardly ever notice it. For me at
least, shadowing builtins is a much more frequent problem/annoyance.
More information about the Python-ideas