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.
python - Lambda expressions with no parameters in Haskell and / or lambda calculus - Software Engineering Stack Exchange
python 3.x - What if the lambda function does not have any argument in python3? - Stack Overflow
python - TypeError: () takes no arguments (1 given) - Stack Overflow
Acceptable use cases for lambda functions without arguments?
Videos
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
Lambda function can be used efficiently even without the lambda variable in python. General procedure for usage of lambda function is as given
in case of general function usage is
def identity(x):
return x+1
in case of lambda
lambda x:x+1
no variable
lambda :x
will return same as x.
Without a variable the lambda function stays idle without any action an hence it can be used without a variable for getting the same answer. In precise usage of lambda without a variable of action is just an extra line of code.But it is valid.
Yes, we can define a lambda function without any argument. But, it will be useless because there will be nothing to operate on. Let’s have a look at a simple example.
get_5 = lambda: 5
print(get_5()) # 5
Since the lambda function is always returning the same value, we can just assign it a variable. Using lambda function without any argument is plain abuse of this feature.
d = collections.defaultdict([-1, -1, -1])
produces the error:
TypeError: first argument must be callable or None
since list isin't callable, but lambda functions are callable.
Scale calls the function passed as command with one argument, so you have to use it (although throw it away immediately).
Change:
command=lambda: scale_changed('LED')
to
command=lambda x: scale_changed('LED')
This is presumably because the command is passed an argument that perhaps you don't want. Try changing the lambda from
command=lambda:scale_changed('LED')
to
command=lambda x:scale_changed('LED')