[Python] Call a function in Python and pass only the arguments it expects

Discussão em 'Python' iniciado por Stack, Fevereiro 4, 2025.

  1. Stack

    Stack Membro Participativo

    How do I call a function and only pass it the arguments that it expects. For example say I have the following functions:

    func1 = lambda a: True
    func2 = lambda a, b: True
    func3 = lambda c: True


    I want some Python code that is able to successfully call these functions without raising a TypeError by passing unexpected arguments. i.e.

    kwargs = dict(a=1, b=2, c=3)
    for func in (func1, func2, func3):
    func(**kwargs) # some magic here


    I'm not interested in just adding **kwargs to the functions when I define them.

    Continue reading...

Compartilhe esta Página