[Python-ideas] relaxing keyword usage restrictions

Chris Rebert pyideas at rebertia.com
Fri Sep 9 20:20:47 CEST 2011


On Fri, Sep 9, 2011 at 10:31 AM, Georg Brandl <g.brandl at gmx.net> wrote:
> Am 09.09.2011 09:48, schrieb Terry Reedy:
>> On 9/9/2011 2:04 AM, H Krishnan wrote:
<snip>
>>> 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.
>
> 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.)

Not always. That code is indeed valid (unless something changed in Python 3.2).

Python 3.1.2:

def foo():
    bar = yield 42 # look Ma, no parens!
    print(bar)

a = foo()
print(next(a))
a.send(7)

Output:
42
7
Traceback (most recent call last):
  File "prog.py", line 7, in <module>
    a.send(7)
StopIteration

Cheers,
Chris



More information about the Python-ideas mailing list