[Python-ideas] Keyword declarations

Carl Smith carl.input at gmail.com
Wed May 16 15:22:28 EDT 2018


My proposal assumes we want to be able to reference the name as defined in
external libraries, but never have it be a name and a keyword in the same
namespace. Your proposal (and the others I've seen) seem to be deliberately
aiming to allow that.

Do you want to have keywords that are names in the same namespace because
that would be nice in itself, or are you only tolerating that to solve the
original problem?

If being free to do `\if if not \not else \else or \or` is something people
want for its own upsides, my proposal is useless, but I thought we only
wanted a way to introduce keywords that are already names.


-- Carl Smith
carl.input at gmail.com

On 16 May 2018 at 20:03, Carl Smith <carl.input at gmail.com> wrote:

> 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.
>
>
> -- Carl Smith
> carl.input at gmail.com
>
> On 16 May 2018 at 18:24, Adam Bartoš <drekin at 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 at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180516/b6d35738/attachment-0001.html>


More information about the Python-ideas mailing list