[Python-ideas] Keyword declarations

Adam Bartoš drekin at gmail.com
Wed May 16 13:24:19 EDT 2018


Hello,

I have yet another idea regarding the the clashes between new keywords and
already used names. How about introducing two new keywords *wink* that
would serve as lexical keyword/nonkeyword declarations, similarly to
nonlocal and global declarations?

def f():
    nonkeyword if
    if = 2 # we use 'if' as an identifier
    def g():
        keyword if
        if x > 0: pass # now 'if' again introduces a conditional statement

This allows to have a name as both identifier and keyword in a single
module, and since it's lexical, it could be in principle syntax-highlighted
correctly.

When a new keyword is added to the list of standard keywords like 'given'
or 'where', a module that uses the name as identifier could be easily fixed
by a global declaration 'nonkeyword given'. Maybe even exception messages
pointing to this could be added. If 'nonkeyword keyword' is allowed, we can
also fix code using the name 'keyword' as an identifier, but doing so in
the global scope couldn't be undone.

On the other hand, new language features depending on new keywords could be
made provisionary by not adding the keywords to the standard list, so
people who would like to use them would need to opt in by e.g. 'keyword
given'. Surely, this provisional mechanism isn't robust at all since new
features may just extend the usage of current keywords.

Declaring a keyword that has no meaning in the language would result in an
exception:

keyword foo # SyntaxError: undefined keyword 'foo'

It should be possible to use a keyword as a parameter name without need to
declare it in the surrounding scope, the local declaration would suffice:

# nonkeyword if # not needed
def f(if=3): # ok
    nonkeyword if

Other option is to interpret parameters always as nonkeywords or to raise a
special syntax error when a keyword occurs at a place of a formal parameter
(similarly to 'def f(x): nonlocal x').

Clearly, even if this proposal diminished the cost of adding new keywords,
the cost would still be high.


Best regards,
Adam Bartoš
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180516/d6746f8b/attachment.html>


More information about the Python-ideas mailing list