You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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. PEP-8 intentionally and properly discourages 'name = lambda ...' as inferior to 'def name(...'.
For your example,
asyncdeffoo(a, b): return5+awaitbar(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.
thomasahle commented on May 31, 2018
thomasahlemannequin
Mannequin
More actions
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:
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.
Coding an self-building Python -> Web-API adapter using lambdas like you would within JS, and the only way to create an async version of my program would be to either:
A: Find some python voodoo to dynamically generate async functions.
B: Build it all by hand and update it manually with each API release.
Being completely honest, neither of these options are desirable.
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.
I found meaningful use cases for async lambda in reactive programming. Here is the example code that should be:
asyncdefread_one_by_id(request: ReadOneByIdRequest) ->Content[Account]:
returnrx.just(request).pipe(
ops.map(asynclambdarequest: awaitaccount_repository.read_one_by_id(request.id)),
ops.map(lambdaentity: Content(data=entity, message="Account read one by id succeed.")),
ops.catch(lambdaexception, source: rx.just(
Content(entity=None, message=f"Account read one by id failed: {exception}")))
).run()
Activity
AceAttorneyMaster111 commentedon May 9, 2018
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:
AceAttorneyMaster111 commentedon May 9, 2018
Actually, you wouldn't even need to import asyncio.
1st1 commentedon May 18, 2018
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.
terryjreedy commentedon May 18, 2018
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. PEP-8 intentionally and properly discourages 'name = lambda ...' as inferior to 'def name(...'.
For your example,
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.
thomasahle commentedon May 31, 2018
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:
where
has_fooandhas_barare async functions.Sometimes it would be useful to combine two functions with
If this function was to be an
async def, rather thanasync 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.
johnandersen777 commentedon May 27, 2022
I also really want async syntax
p0t4t0sandwich commentedon Oct 28, 2022
Coding an self-building Python -> Web-API adapter using lambdas like you would within JS, and the only way to create an async version of my program would be to either:
A: Find some python voodoo to dynamically generate async functions.
B: Build it all by hand and update it manually with each API release.
Being completely honest, neither of these options are desirable.
muazhari commentedon Feb 13, 2023
I found meaningful use cases for async lambda in reactive programming. Here is the example code that should be: