What is right syntax to call lambda directly when it is defined

I’m trying to call a lambda directly when it is defined. Is the purpose of the first pair of parentheses is to stop x (3) from coming together. Because () is higher than lambda in precedence?


>>> (lambda x: x) (3)
>>> a = lambda x: x (3)
>>> b = lambda x: x + 2
>>> a(b)
1 Like

Yes. Those parentheses prevent its being interpreted as an attempt to call a function named x within the lambda.

EDITED for clarity on March 12, 2022.

1 Like

Thanks Quercus

1 Like

In summary …

  1. Place the entire definition of the lambda in parentheses.
  2. After the closing parenthesis in the above, place the argument(s) to be passed to the lambda in parentheses.
1 Like