[Python-ideas] relaxing keyword usage restrictions

Georg Brandl g.brandl at gmx.net
Fri Sep 9 21:23:45 CEST 2011

Am 09.09.2011 20:21, schrieb Jacob Holm:
> On 2011-09-09 19:31, Georg Brandl wrote:
>> Am 09.09.2011 09:48, schrieb Terry Reedy:
>>> On 9/9/2011 2:04 AM, H Krishnan wrote:
> [snip]
>>> 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.
>> Actually, it isn't: "yield" expressions, like generator expressions, need
>> to be inside parentheses.  (A rule that's an ambiguousness restriction and
>> a nice readability helper.)
> Actually it is, at least in 3.1 (I don't have a more recent version to
> test on ATM).  It is the same as
>    myreturn = yield (-principal)
> It computes and yields the value (-principal).  When resumed by a "send"
> on the generator the argument is assigned to myreturn.  If resumed by
> "next" instead, this is treated as send(None).

Of course.  I'm not new to PEP 342 :)  But I have to apologize: what I did
test was the confusingly similar

    return yield - principal

which isn't allowed (yes, I know that even

    return (yield -principal)

isn't allowed, but that's not for syntactical reasons.)

Now I checked properly: In fact, "yield" expressions after assignment
operators are special-cased by the grammar, so that they don't need to
be parenthesized [1]. In all other places, yield expressions must occur
in parentheses.  For example:

    myreturn = principal - yield


[1] I guess that's because it was thought to be a common case.  I agree
    that it's not really helping readability.

More information about the Python-ideas mailing list