a lambda has nothing to do with loop. It's just some inline way to define a function.
Instead of using a lambda you could have written the code below. The mere difference with using a lambda is that the function defined get a name.
def func(n):
print 'The count is:', n
def mylambda(count):
return (count < 5 and func())
a = mylambda
print a
Now may be you can see by yourself what's wrong ?
- just writing 'a' does not call the function (parenthesis needed)
- your lambda need a parameter for count
- there is no loop, you are just computing a boolean combining the predicate 'count < 5' and the result of calling func()
- func returns nothing, henceforth it will always return None
- you get a function that will return either False or None depending of the value of the parameter count...
Actually, I'm still wondering what you were trying to do ? Call a lambda in a loop ? Create a recursive lambda ? Anyone's guess until more details.
Answer from kriss on Stack Overflowa lambda has nothing to do with loop. It's just some inline way to define a function.
Instead of using a lambda you could have written the code below. The mere difference with using a lambda is that the function defined get a name.
def func(n):
print 'The count is:', n
def mylambda(count):
return (count < 5 and func())
a = mylambda
print a
Now may be you can see by yourself what's wrong ?
- just writing 'a' does not call the function (parenthesis needed)
- your lambda need a parameter for count
- there is no loop, you are just computing a boolean combining the predicate 'count < 5' and the result of calling func()
- func returns nothing, henceforth it will always return None
- you get a function that will return either False or None depending of the value of the parameter count...
Actually, I'm still wondering what you were trying to do ? Call a lambda in a loop ? Create a recursive lambda ? Anyone's guess until more details.
The answer to your problem lies in the fine print
See section Function definitions for the syntax of parameter lists. Note that functions created with lambda expressions cannot contain statements.
and while is part of Compund Statement
To Simplify,
If you can write your function in the form
def name(arguments):
return expression
You can convert it to lambda as
name = lambda arguments: expression
I am trying to implement while loop using lambdas
You should seriously consider using itertools.dropwhile or itertools.takewhile
python - loop for inside lambda - Stack Overflow
python - Lambda in a loop - Stack Overflow
amazon web services - Cannot run a While Loop in AWS Python Lambda - Stack Overflow
How to run a while loop in python using a Lambda - Stack Overflow
Videos
Just in case, if someone is looking for a similar problem...
Most solutions given here are one line and are quite readable and simple. Just wanted to add one more that does not need the use of lambda(I am assuming that you are trying to use lambda just for the sake of making it a one line code). Instead, you can use a simple list comprehension.
[print(i) for i in x]
BTW, the return values will be a list of Nones.
Since a for loop is a statement (as is print, in Python 2.x), you cannot include it in a lambda expression. Instead, you need to use the write method on sys.stdout along with the join method.
x = lambda x: sys.stdout.write("\n".join(x) + "\n")
You need to bind d for each function created. One way to do that is to pass it as a parameter with a default value:
lambda d=d: self.root.change_directory(d)
Now the d inside the function uses the parameter, even though it has the same name, and the default value for that is evaluated when the function is created. To help you see this:
lambda bound_d=d: self.root.change_directory(bound_d)
Remember how default values work, such as for mutable objects like lists and dicts, because you are binding an object.
This idiom of parameters with default values is common enough, but may fail if you introspect function parameters and determine what to do based on their presence. You can avoid the parameter with another closure:
(lambda d=d: lambda: self.root.change_directory(d))()
# or
(lambda d: lambda: self.root.change_directory(d))(d)
This is due to the point at which d is being bound. The lambda functions all point at the variable d rather than the current value of it, so when you update d in the next iteration, this update is seen across all your functions.
For a simpler example:
funcs = []
for x in [1,2,3]:
funcs.append(lambda: x)
for f in funcs:
print f()
# output:
3
3
3
You can get around this by adding an additional function, like so:
def makeFunc(x):
return lambda: x
funcs = []
for x in [1,2,3]:
funcs.append(makeFunc(x))
for f in funcs:
print f()
# output:
1
2
3
You can also fix the scoping inside the lambda expression
lambda bound_x=x: bound_x
However in general this is not good practice as you have changed the signature of your function.