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
Title: Asynchronous lambda syntax
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: postponed
Dependencies: Superseder:
Assigned To: Nosy List: Noah Simon, asvetlov, terry.reedy, thomasahle, yselivanov
Priority: normal Keywords:

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) * Date: 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) * Date: 2018-05-09 03:24
Actually, you wouldn't even need to import asyncio.
msg317056 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 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) Date: 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) * Date: 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.
History
Date User Action Args
2022-04-11 14:59:00adminsetgithub: 77628
2018-05-31 09:55:25thomasahlesetnosy: + thomasahle
messages: + msg318279
2018-05-18 21:44:04terry.reedysetnosy: + terry.reedy
messages: + msg317061
2018-05-18 19:55:06yselivanovsetstatus: open -> closed
versions: + Python 3.8, - Python 3.7
messages: + msg317056

components: + Interpreter Core, - asyncio
resolution: postponed
stage: resolved
2018-05-09 03:24:33Noah Simonsetmessages: + msg316305
2018-05-09 03:22:40Noah Simoncreate