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
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', ), ('y', ), ('z', )]))