[Python-Dev] Issues with PEP 482 (1)
Mark Shannon
mark at hotpy.org
Tue Apr 28 20:44:53 CEST 2015
Hi,
I still think that there are several issues that need addressing with
PEP 492. This time, one issue at a time :)
"async"
The "Rationale and Goals" of PEP 492 states that PEP 380 has 3 shortcomings.
The second of which is:
"""It is not possible to natively define a coroutine which has no
yield or yield from statements."""
This is incorrect, although what is meant by 'natively' is unclear.
A coroutine without a yield statement can be defined simply and
concisely, thus:
@coroutine
def f():
return 1
This is only a few character longer than the proposed new syntax,
perfectly explicit and requires no modification the language whatsoever.
A pure-python definition of the "coroutine" decorator is given below.
So could the "Rationale and Goals" be correctly accordingly, please.
Also, either the "async def" syntax should be dropped, or a new
justification is required.
Cheers,
Mark.
#coroutine.py
from types import FunctionType, CodeType
CO_COROUTINE = 0x0080
CO_GENERATOR = 0x0020
def coroutine(f):
'Converts a function to a generator function'
old_code = f.__code__
new_code = CodeType(
old_code.co_argcount,
old_code.co_kwonlyargcount,
old_code.co_nlocals,
old_code.co_stacksize,
old_code.co_flags | CO_GENERATOR | CO_COROUTINE,
old_code.co_code,
old_code.co_consts,
old_code.co_names,
old_code.co_varnames,
old_code.co_filename,
old_code.co_name,
old_code.co_firstlineno,
old_code.co_lnotab,
old_code.co_freevars,
old_code.co_cellvars)
return FunctionType(new_code, f.__globals__)
P.S. The reverse of this decorator, which unsets the flags, converts a
generator function into a normal function. :?
More information about the Python-Dev
mailing list