← 输入框|github.com|刷新
Skip to content

Asynchronous lambda syntax #77628

Closed
Closed

Description

@AceAttorneyMaster111
BPO 33447
Nosy @terryjreedy, @asvetlov, @1st1, @AceAttorneyMaster111

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-05-18.19:55:06.834>
created_at = <Date 2018-05-09.03:22:40.128>
labels = ['interpreter-core', 'type-feature', '3.8']
title = 'Asynchronous lambda syntax'
updated_at = <Date 2018-05-31.09:55:25.848>
user = 'https://github.com/AceAttorneyMaster111'

bugs.python.org fields:

activity = <Date 2018-05-31.09:55:25.848>
actor = 'thomasahle'
assignee = 'none'
closed = True
closed_date = <Date 2018-05-18.19:55:06.834>
closer = 'yselivanov'
components = ['Interpreter Core']
creation = <Date 2018-05-09.03:22:40.128>
creator = 'Noah Simon'
dependencies = []
files = []
hgrepos = []
issue_num = 33447
keywords = []
message_count = 5.0
messages = ['316304', '316305', '317056', '317061', '318279']
nosy_count = 5.0
nosy_names = ['terry.reedy', 'asvetlov', 'thomasahle', 'yselivanov', 'Noah Simon']
pr_nums = []
priority = 'normal'
resolution = 'postponed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue33447'
versions = ['Python 3.8']

Activity

  1. AceAttorneyMaster111 commented on May 9, 2018

    AceAttorneyMaster111mannequin
    MannequinAuthor

    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)
  2. added
    3.7 (EOL)
    type-feature
    on May 9, 2018
  3. AceAttorneyMaster111 commented on May 9, 2018

    AceAttorneyMaster111mannequin
    MannequinAuthor

    Actually, you wouldn't even need to import asyncio.

  4. 1st1 commented on May 18, 2018

    @1st1
    Member

    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.

  5. added
    interpreter-core
    3.8 (EOL)
    and removed
    3.7 (EOL)
    on May 18, 2018
  6. terryjreedy commented on May 18, 2018

    @terryjreedy
    Member

    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,

    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.

  7. thomasahle commented on May 31, 2018

    thomasahlemannequin
    Mannequin

    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.

  8. transferred this issue fromon Apr 10, 2022
  9. johnandersen777 commented on May 27, 2022

    @johnandersen777

    I also really want async syntax

  10. p0t4t0sandwich commented on Oct 28, 2022

    @p0t4t0sandwich

    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.

  11. muazhari commented on Feb 13, 2023

    @muazhari

    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:

    async def read_one_by_id(request: ReadOneByIdRequest) -> Content[Account]:
        return rx.just(request).pipe(
            ops.map(async lambda request: await account_repository.read_one_by_id(request.id)),
            ops.map(lambda entity: Content(data=entity, message="Account read one by id succeed.")),
            ops.catch(lambda exception, source: rx.just(
                Content(entity=None, message=f"Account read one by id failed: {exception}")))
        ).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.8 (EOL)interpreter-coretype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions