[Python] How to add function parameter kwargs during runtime?

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have hundreds of functions test1, test2, ...

    Function testX may or may not have kwargs:

    def test1(x, z=1, **kwargs):
    pass
    def test2(x, y=1):
    pass
    def test3(x, y=1, z=1):
    pass
    ...


    and I have a function call_center:

    tests = [test1, test2, test3]
    def call_center(**kwargs):
    for test in tests:
    test(**kwargs)


    call_center will be called with various input parameters:

    call_center(x=1,y=1)
    call_center(x=1,z=1)


    I don't want to filter kwargs by inspect.signature of the called function, because these function will be called millions of times, filtering at runtime costs a lot.

    How can I define a decorator extend_kwargs that adds kwargs to functions that do not have kwargs?

    Continue reading...

Compartilhe esta Página