[Python-ideas] relaxing keyword usage restrictions
jh at improva.dk
Fri Sep 9 20:21:23 CEST 2011
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:
>> 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).
I agree that this is confusing. In spite of all my work hashing out the
details of PEP 380, I still initially thought it would work like
myreturn = (yield) - principal
probably because of the context where "yield" was supposed to be a
regular variable name. (An idea I'm -1 on, btw).
Based on this, I would support a change to outlaw
myreturn = yield - principal
forcing you to add the parentheses to clearly show which interpretation
However, I would still want to keep the statement form
legal, so I am not sure if that proposal has a chance to fly.
More information about the Python-ideas