I know that when we use *args, it means we are not sure how many arguments the function is going to receive. However, Python finally binds them together in a single tuple:
>>> def f(*args):
return type(args)
>>> f(3,4,4,5)
<class 'tuple'>
Suppose I have a simple function that returns the input unchanged. I can use it with a nested lambda like this:
>>> def f (x):
return x
>>> l = f(lambda x: len(x))
>>>
>>> l((1,2,3))
3
Note that the input is a tuple. However, this is what happens when I try to write the same function with args:
>>> def f (*args):
return args
>>> l = f(lambda x: len(x))
>>> l((1,2,3))
Traceback (most recent call last):
File "<pyshell#106>", line 1, in <module>
l((1,2,3))
TypeError: 'tuple' object is not callable
Why do I receive this error and how should I avoid it?
In the second example, l is not being assigned to what you think it is:
>>> def f (*args):
... return args
...
>>> l = f(lambda x: len(x))
>>> l
(<function <lambda> at 0x020518A0>,)
>>>
As you can see, it is a tuple that contains the lambda function. This is expected though because *args always collects its arguments into a tuple, which the function f then returns.
Later, when you do:
l((1,2,3))
a TypeError is raised for trying to call the tuple l as a function.
You can avoid this error by simply indexing the tuple to extract the lambda function:
l = f(lambda x: len(x))[0]
Now l refers to what it should:
>>> def f (*args):
... return args
...
>>> l = f(lambda x: len(x))[0]
>>> l
<function <lambda> at 0x020169C0>
>>> l((1,2,3))
3
>>>
Although, as @abarnert said in his comment, it looks like you are actually trying to make l take a variable number of arguments. If so, then you need to use *args on the lambda, not function f:
>>> def f(x):
... return x
...
>>> l = f(lambda *args: len(args))
>>> l(1, 2, 3, 4, 5)
5
>>>
Remember that when you do l(...), it is the lambda that is being called. So, any arguments you give to l will be sent there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With