[Python-ideas] Keyword declarations

Terry Reedy tjreedy at udel.edu
Wed May 16 14:37:24 EDT 2018


On 5/16/2018 1:24 PM, Adam Bartoš 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

These are not at all similar to 'global' and 'nonlocal'.  These would 
make Python into a syntactically context-dependent language, rather than 
a restricted (ll(1)) context-free language.  I am pretty sure that the 
current parser generator could not handle this.

'Global' and 'nonlocal' have NO effect on what is syntactically legal 
and therefore no effect on syntax parsing.  They only affect the meaning 
(semantics) of other statements within the same function.  The compiler 
handles this by making two passes over a function body.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list