Determining the number of parameters in a lambda

前端 未结 3 1956
你的背包
你的背包 2021-01-31 02:43

I am wondering if there is a way to determine (given a variable containing a lambda) the number of parameters the lambda it contains. The reason being, I wish to call a functio

3条回答
  •  广开言路
    2021-01-31 02:59

    Lambdas are functions like any other. The argument count is stored in func.__code__.co_argcount.

    >>> foo = lambda x, y=2: x+y
    >>> foo.__code__.co_argcount
    2
    >>> foo = lambda x, y=2, z=3: x+y+z
    >>> foo.__code__.co_argcount
    3
    

提交回复
热议问题