Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the number of parameters in a lambda

Tags:

python

lambda

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 function conditionally dependent on the number of parameters.

What I'm looking for

def magic_lambda_parameter_counting_function(lambda_function):
    """Returns the number of parameters in lambda_function

    Args:
        lambda_function - A lambda of unknown number of parameters
    """

So I can do something like

def my_method(lambda_function):

    # ... 
    # (say I have variables i and element)

    parameter_count = magic_lambda_parameter_counting_function(lambda_function)

    if parameter_count == 1:
        lambda_function(i)
    elif parameter_count == 2:
        lambda_function(i, element)
like image 221
Anthony Sottile Avatar asked Jun 02 '12 19:06

Anthony Sottile


People also ask

How many parameters does lambda have?

The lambda expressions are easy and contain three parts like parameters (method arguments), arrow operator (->) and expressions (method body).

What is the parameter lambda?

A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

Can a lambda function have no parameters?

Python lambda function with no argumentsYes, we can define a lambda function without any argument.

Can you pass parameters to lambda function?

To configure a REST API to pass query string parameters to a backend Lambda function, use a Lambda custom integration. To pass query string parameters to an HTTP endpoint, use an HTTP custom integration. Important:Make sure that the input data is supplied as the integration request payload.


2 Answers

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
like image 120
BrenBarn Avatar answered Oct 16 '22 07:10

BrenBarn


I'm skipping the part about how to count the arguments, because I don't know how you want to consider varargs and keywords. But this should get you started.

>>> import inspect
>>> foo = lambda x, y, z: x + y + z
>>> inspect.getargspec(foo)
ArgSpec(args=['x', 'y', 'z'], varargs=None, keywords=None, defaults=None)

It sounds like inspect.getargspec is deprecated as of Python 3 (thanks to @JeffHeaton). The recommend solution uses inspect.signature. The .parameters member of the result contains a variety of structures depending on the arrangement of parameters to the function in question, but I'm matching my function from the Python 2 example above.

>>> import inspect
>>> foo = lambda x, y, z: x + y + z
>>> inspect.signature(foo).parameters
mappingproxy(OrderedDict([('x', <Parameter "x">), ('y', <Parameter "y">), ('z', <Parameter "z">)]))
like image 45
robert Avatar answered Oct 16 '22 07:10

robert