A lambda function (or more accurately, a lambda expression) is simply a function you can define on-the-spot, right where you need it. For example,
f = lambda x: x * 2
is exactly the same thing as
def f(x):
return x * 2
And when I say exactly, I mean it -- they disassemble to the same bytecode. The only difference between the two is that the one defined in the second example has a name.
Lambda expressions become useful because creating one is not a statement, which means that, as others have already answered, you can do
print iterator(lambda x: x / 4 + 12, 100, 5)
to get precisely what you want.
The main difference between lambda expressions and regular functions, however, is that lambdas are more limited. Lambdas can only contain expressions, not statements. An expression is anything you can put on the right side of an = assignment. (if you want to get more pedantic, Python defines an expression as http://docs.python.org/2/reference/expressions.html )
What this means is a lambda expression can not assign to a variable (in fact, it can't have local variables at all, other than its parameters). It can't print (unless it calls another function that does). It can't have a for loop, a while loop, an if test (other than the ternary operator x if cond else y), or a try/except block.
If you need to do any of those, just define a regular function. In fact, any time you think you want to use a lambda, think twice. Wouldn't the code be more readable if you used a regular function? Isn't that lambda expression something you'd like to reuse somewhere else in your code?
In the end, always do what leads to the most readable and maintainable code. There is no difference between lambdas and normal functions as far as performance is concerned.
Answer from Max Noel on Stack OverflowA lambda function (or more accurately, a lambda expression) is simply a function you can define on-the-spot, right where you need it. For example,
f = lambda x: x * 2
is exactly the same thing as
def f(x):
return x * 2
And when I say exactly, I mean it -- they disassemble to the same bytecode. The only difference between the two is that the one defined in the second example has a name.
Lambda expressions become useful because creating one is not a statement, which means that, as others have already answered, you can do
print iterator(lambda x: x / 4 + 12, 100, 5)
to get precisely what you want.
The main difference between lambda expressions and regular functions, however, is that lambdas are more limited. Lambdas can only contain expressions, not statements. An expression is anything you can put on the right side of an = assignment. (if you want to get more pedantic, Python defines an expression as http://docs.python.org/2/reference/expressions.html )
What this means is a lambda expression can not assign to a variable (in fact, it can't have local variables at all, other than its parameters). It can't print (unless it calls another function that does). It can't have a for loop, a while loop, an if test (other than the ternary operator x if cond else y), or a try/except block.
If you need to do any of those, just define a regular function. In fact, any time you think you want to use a lambda, think twice. Wouldn't the code be more readable if you used a regular function? Isn't that lambda expression something you'd like to reuse somewhere else in your code?
In the end, always do what leads to the most readable and maintainable code. There is no difference between lambdas and normal functions as far as performance is concerned.
Yes, you can just use lambda expressions. They are made for this.
iterator(lambda x: x/4+12, 100, 5)
Words from the docs:
Lambdas are usually used to create small, anonymous functions. Actually, they are just a syntatic sugar to define functions. The lambda expression above is exactly the same as your function, only without a name.
If you wish to learn more, Here is some good read:
http://www.diveintopython.net/power_of_introspection/lambda_functions.html
Why use lambda functions?
I'm trying to learn and use lambda functions, but I'm terribly confused. I understand the examples in tutorials that explicitly pass in values into lambda functions, but I'm stumped by real examples.
For instance, I came across a Reddit post that had the following example:
my_list = [(1, 3), (2, 1), (3, 2)] sorted(my_list, key=lambda x: x[1]) [(2, 1), (3, 2), (1, 3)]
How does the lambda function take in the second argument as x? Where is that denoted? How do I go about reading lambda functions like this?
Understanding lambda in Python and using it to pass multiple arguments - Stack Overflow
How to pass a parameter in a lambda function in python? - Stack Overflow
How do arguments get passed into lambda functions?
How do I pass a lambda to a method as a parameter?
Videos
Why do you need to state both 'x' and 'y' before the ':'?
Because a lambda is (conceptually) the same as a function, just written inline. Your example is equivalent to
def f(x, y) : return x + y
just without binding it to a name like f.
Also how do you make it return multiple arguments?
The same way like with a function. Preferably, you return a tuple:
lambda x, y: (x+y, x-y)
Or a list, or a class, or whatever.
The thing with self.entry_1.bind should be answered by Demosthenex.
I believe bind always tries to send an event parameter. Try:
self.entry_1.bind("<Return>", lambda event: self.calculate(self.buttonOut_1.grid_info(), 1))
You accept the parameter and never use it.
The command callable doesn't take any arguments. If you want to pass the green list into printfunction, just omit the argument, the lambda doesn't need it:
Btn=Button(text="Trigger lambda", command=lambda: printfunction(green))
Now green inside the lambda refers to the global.
If all you wanted to do was to call printfunction with a pre-defined argument, you could use the functools.partial() function; you pass it the function to be called plus any arguments that need to be passed in, and when it's return value is called, it'll do exactly that; call the function with the arguments you specified:
from functools import partial
Btn=Button(text="Trigger lambda", command=partial(printfunction, green))
Python is incredibly dynamic, you can modify classes after their definition or create local functions. Example:
class X:
# generic print function
def print_color(self, color):
pass
# dictionary with colors to support
COLORS = {'green': ["#5bd575","#55c76d"]}
# attach various print functions to class X
for name, value in COLORS.items():
def print_color_x(self, value=value):
self.print_color(value)
setattr(X, 'print_color_{}'.format(name), print_color_x)
Note the value=value default parameter. This one is necessary to bind the value within each iteration. Without it, the lookup of value would take place when the function is called, giving you errors about a missing global value or picks up a random one that it happens to find there, but not the one you want. Using functools.partial() could be cleaner, if it allowed creating proper memberfunctions and not just staticfunctions. Note that the example pure Python implementation mentioned in the documentation allows creating memberfunctions, so using that as a replacement is an option.