If `def(if=3)...` works implicitly, then why not make `if = 3`, `x.if = 3`, `import if`, `def if` and `class if` implicit too?

Another issue is what happens here:

keyword if
import if
f(if=3)
f.if = 3

The keyword will be a valid name in old code, so you need to be able to reference it as a name in code that uses it as a keyword.



On 16 May 2018 at 18:24, Adam Bartoš <drekin@gmail.com> wrote:
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š


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