[Python-ideas] relaxing keyword usage restrictions

Paul Moore p.f.moore at gmail.com
Fri Sep 9 10:11:23 CEST 2011


On 9 September 2011 08:48, Terry Reedy <tjreedy at udel.edu> wrote:
>> One cannot however write readable code such as the following:
>> if yield > principal:
>>    return = yield - principal
>
> Funny you should choose that example. With a slight change
>  myreturn = yield - principal
> it is legal syntax today with 'yield' interpreted as a keyword. So it cannot
> be interpreted as an identifier without making Python grammar ambiguous and
> unparseable with its current parser.

Here is some currently legal syntax:

globals()['return'] = lambda x:x
def f():
    return(12)

print(f())

Currently, that prints 12. If keywords are allowed as variable names,
presumably it should produce 'None'? Or should we break the semantics
of globals()? Or what?

And given that the globals() line could be arbitrarily far away from
the definition of f(), the parser would need arbitrary lookbehind. And
lookahead, as the globals() line could be in a function defined after
f but called before it.

If you don't like me using globals(), the line could be replaced by

def g():
    global return
    return = lambda x:x
g()

but that's not currently legal syntax, so it obscures my point slightly...

Python's parser is simple by design. This isn't going to be accepted
(even if it were a good idea :-))

Paul.



More information about the Python-ideas mailing list