← 输入框|bugs.python.org|刷新

➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
标题: Asynchronous lambda syntax
类型: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
状态: closed Resolution: postponed
Dependencies: 后续:
分配给: 抄送列表: Noah Simon, asvetlov, terry.reedy, thomasahle, yselivanov
优先级: normal 关键字:

Created on 2018-05-09 03:22 by Noah Simon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg316304 - (view) Author: Noah Simon (Noah Simon) * 日期: 2018-05-09 03:22
It would be very useful to add an asynchronous lambda syntax, as a shortcut for coroutines. I'm not experienced enough to write a PEP or edit the C source, but I have some ideas for syntax:

import asyncio
foo = async lambda a,b: 5 + await bar(b)
msg316305 - (view) Author: Noah Simon (Noah Simon) * 日期: 2018-05-09 03:24
Actually, you wouldn't even need to import asyncio.
msg317056 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) 日期: 2018-05-18 19:55
The syntax for async lambdas doesn't look nice and I, personally, don't see that many use cases for them to justify adding new syntax.  And this would need a new PEP. 

I suggest to start a discussion on the Python-ideas mailing list if this is something you want to see in Python.
msg317061 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) 日期: 2018-05-18 21:44
The only thing special about functions defined with lambda expressions rather than def statements is the generic name attribute '<lambda>' instead of a specific name.  PEP8 intentionally and properly discourages 'name = lambda ...' as inferior to 'def name(...'.

For your example, 

async def foo(a, b): return 5 + await bar(b)

For inline function expressions, as in function calls, the obvious proposals would be to reuse 'async' in f(async lambda a, b: ...) or a new keyword, such as 'alambda' or 'asynclambda' or ... . Either would need multiple use cases to justify not just using async def first.
msg318279 - (view) Author: Thomas Dybdahl Ahle (thomasahle) * 日期: 2018-05-31 09:55
Just wanted to add another use-case. In a project I'm working on, we are building a lot of graphs using code like this:

```
nodes = [
    Node('node-name1',
         children=[...],
         classifier=has_foo),
    Node('node-name2',
         children=[...],
         classifier=has_bar),
    ...
]
```
where `has_foo` and `has_bar` are async functions.
Sometimes it would be useful to combine two functions with
```
    Node('node-name',
         children=[...],
         classifier=async lambda: x: await has_bar(x) or await has_foo(x))
```
If this function was to be an `async def`, rather than `async lambda`, it would have to be defined far away from where it is used. This doesn't always make sense semantically.

I don't think this example on its own is enough to warrant new syntax in the language, but if somebody is collecting "multiple use cases to justify not just using async def first", perhaps it's worth including.
历史
日期 用户 动作 参数
2022-04-11 14:59:00admin修改github: 77628
2018-05-31 09:55:25thomasahle修改抄送: + thomasahle
消息: + msg318279
2018-05-18 21:44:04terry.reedy修改抄送: + terry.reedy
消息: + msg317061
2018-05-18 19:55:06yselivanov修改状态: open -> closed
versions: + Python 3.8, - Python 3.7
消息: + msg317056

components: + Interpreter Core, - asyncio
resolution: postponed
stage: resolved
2018-05-09 03:24:33Noah Simon修改消息: + msg316305
2018-05-09 03:22:40Noah Simon创建