The _ is variable name. Try it.
(This variable name is usually a name for an ignored variable. A placeholder so to speak.)
Python:
>>> l = lambda _: True
>>> l()
<lambda>() missing 1 required positional argument: '_'
>>> l("foo")
True
So this lambda does require one argument. If you want a lambda with no argument that always returns True, do this:
>>> m = lambda: True
>>> m()
True
Answer from user1907906 on Stack OverflowThe _ is variable name. Try it.
(This variable name is usually a name for an ignored variable. A placeholder so to speak.)
Python:
>>> l = lambda _: True
>>> l()
<lambda>() missing 1 required positional argument: '_'
>>> l("foo")
True
So this lambda does require one argument. If you want a lambda with no argument that always returns True, do this:
>>> m = lambda: True
>>> m()
True
Underscore is a Python convention to name an unused variable (e.g. static analysis tools does not report it as unused variable). In your case lambda argument is unused, but created object is single-argument function which always returns True. So your lambda is somewhat analogous to Constant Function in math.
I’ve seen several sources that say using Python’s lambda functions without any arguments is a bad idea and “abuse of this feature”, but I think there are (at least) 2 valid use cases for it:
When writing a function that has a callable as an argument, but you want the same output from it every time. For example, collections.defaultdict takes in a callable and a map as arguments. If I’m making some sort of game AI, having the default always be infinity or negative infinity can be useful when designing its decision making.
When writing a dictionary/list where the values/elements are functions. You want to do something that’s not just returning a value when you call dictkey/listindex but you don’t want to define another function, and it you don’t want to take in an argument because another function in the dict already doesn’t.
Do you agree with this or are there much simpler solutions to this?
Well, the other answers cover what \() -> "something" means in Haskell: an unary function that takes () as argument.
What is a function without arguments? – A value. Actually, it can occasionally be useful to think of variables as nullary functions that evaluate to their value. The
let-syntax for a function without arguments (which doesn't actually exist) ends up giving you a variable binding:let x = 42 in ...Does lambda calculus have nullary functions? – No. Every function takes exactly one argument. However, this argument may be a list, or the function may return another function that takes the next argument. Haskell prefers the latter solution, so that
a b cis actually two function calls((a b) c). To simulate nullary functions, you have to pass some unused placeholder value.
You're misinterpreting what () means in Haskell. It isn't the lack of a value, it is rather the only value of the Unit type (the type itself being referred to by an empty set of parentheses ()).
Since lambdas can be constructed to use pattern matching, the lambda expression \() -> "s" is explicitly saying "create an anonymous function, expecting an input that matches the () pattern". There isn't much point to doing it, but it's certainly allowed.
You can use pattern matching with lambdas in other ways as well, for example:
map (\(a, b) -> a + b) [(1,2), (3,4), (5,6)] -- uses pattern matching to destructured tuples
map (\(Name first _) -> first) [Name "John" "Smith", Name "Jane" "Doe"] -- matches a "Name" data type and its first field
map (\(x:_) -> x) [[1,2,3], [4,5,6]] -- matches the head of a list
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.
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.
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?