+ 1

parameter x in lambda function ?

example, in: print(list(takewhile(lambda x: x<= 6, nums))) lambda x: #is fonctiun name, but in x<= 6 , where is x defined? i dont understand well ?

25th Jun 2020, 2:09 PM
Alain Mazy
Alain Mazy - avatar
3 Respuestas
+ 5
This is how this lambda is working: - lambda always starts with the keyword 'lambda' - 'x:' is a variable definition (argument), can have each other valid var name. This is like a variable in the header of a regular function and can take values - 'x<= 6' is a condition to check - nums is the name of a iterable e.g. a list that will be used by takewhile() > takewhile() is an iterator that uses one elements from nums per iteration, starting from index 0, as long as they are less or equal to 6 and passes them to a list that is printed. Then the iteration stops
25th Jun 2020, 6:15 PM
Lothar
Lothar - avatar
+ 3
lambda => anonym function That means that x isn't the name of the lambda (because by definition a lambda has no name), but the first parameter of the lambda.
25th Jun 2020, 2:13 PM
Théophile
Théophile - avatar
+ 1
Thans for your replys.. i undestand ;)
18th Jul 2020, 12:57 PM
Alain Mazy
Alain Mazy - avatar