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 Overflow
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Python Examples Python Compiler ... A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression....
Discussions

python - Lambda expressions with no parameters in Haskell and / or lambda calculus - Software Engineering Stack Exchange
So, I was expecting that Haskell ... there is no need to build delayed expressions. To my surprise, I found out that in Haskell it is possible to write the lambda expression ... Applying this function to any argument other than () throws an exception (at least as far as I could see during my tests). This seems different from delayed evaluation in Scheme and Python, because the ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
September 23, 2014
python 3.x - What if the lambda function does not have any argument in python3? - Stack Overflow
I recently read a line of code and do not understand what is it meaning, the code is · import collections d = collections.defaultdict(lambda : [-1, -1, -1]) I'm confused about the lambda here. I only know lamnda x: x+1. Usually, we have an argument for lambda. What if we don't have argument like this code? ... Save this answer. Show activity on this post. Lambda function can be used efficiently even without the lambda variable in python... More on stackoverflow.com
🌐 stackoverflow.com
python - TypeError: () takes no arguments (1 given) - Stack Overflow
Try changing the lambda from ... command - A procedure to be called every time the slider is moved. This procedure will be passed one argument, the new scale value. If the slider is moved rapidly, you may not get a callback for every possible position, but you'll certainly get a callback when ... More on stackoverflow.com
🌐 stackoverflow.com
April 25, 2017
Acceptable use cases for lambda functions without arguments?
Imo you’ve found some outliers - and I wouldn’t worry about why others think! While True: Has use cases after all More on reddit.com
🌐 r/Python
7
6
December 5, 2022
🌐
Reddit
reddit.com › r/python › acceptable use cases for lambda functions without arguments?
r/Python on Reddit: Acceptable use cases for lambda functions without arguments?
December 5, 2022 -

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:

  1. 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.

  2. 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?

🌐
DEV Community
dev.to › divshekhar › python-lambda-function-po3
Python Lambda Function - DEV Community
June 19, 2023 - A lambda function in Python can have any number of arguments, including zero arguments. Lambda function with no arguments performs a simple operation without needing any inputs.
🌐
Afternerd
afternerd.com › blog › python-lambdas
Python Lambdas Explained (With Examples) - Afternerd
April 6, 2019 - Alright now that I have laid down some foundations, let’s talk about lambdas. A Python lambda is just another method to define a function. ... Lambda functions can accept zero or more arguments but only one expression.
🌐
Python Examples
pythonexamples.org › python-lambda-function-without-arguments
Python Lambda Function without Arguments
In the following program, we define a lambda function without any arguments, and that always returns a string value of "Hello World". We shall assign the lambda function to a variable named greeting. Call the lambda function with greeting() and print the returned value. greeting = lambda: "Hello ...
Find elsewhere
Top answer
1 of 3
14

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 c is actually two function calls ((a b) c). To simulate nullary functions, you have to pass some unused placeholder value.

2 of 3
9

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
🌐
Tutorial Teacher
tutorialsteacher.com › python › python-lambda-function
Lambda Functions and Anonymous Functions in Python
The lambda keyword is used to define anonymous functions in Python. Usually, such a function is meant for one-time use. ... The lambda function can have zero or more arguments after the : symbol.
🌐
DataFlair
data-flair.training › blogs › python-lambda-expression
Python Lambda Expression - Declaring Lambda Expression & Its Defaults - DataFlair
March 8, 2021 - Other things like an assignment statement cannot be provided as an expression to Python lambda because it does not return anything, not even None. Like we’ve seen so far, Python lambda expression may take arguments, and takes one expression.
🌐
The Python Coding Stack
thepythoncodingstack.com › p › whats-all-the-fuss-about-python-lambda-functions
What's All the Fuss About `lambda` Functions in Python
December 1, 2023 - Instead, you enclose the expression in parentheses, and you can treat it just like you would treat a standard function name: add (10) to call the function with 10 as the argument. You could name a lambda function if you really wanted to. But if you feel the need to name a lambda function, you probably should use a standard function instead. Anyway, here's how you would name a lambda function if you're stubborn enough to do so: And that's all there is, really. Python's lambda functions are just functions with no name...
🌐
AskPython
askpython.com › home › python lambda – anonymous function
Python lambda - Anonymous Function - AskPython
September 5, 2019 - We can define an anonymous function using the lambda reserved keyword. The anonymous function scope is limited to the current scope where it’s defined. A lambda function can have one or more arguments but it can have only one expression.
🌐
Real Python
realpython.com › python-lambda
How to Use Python Lambda Functions – Real Python
December 1, 2023 - The lambda function assigned to full_name takes two arguments and returns a string interpolating the two parameters first and last. As expected, the definition of the lambda lists the arguments with no parentheses, whereas calling the function is done exactly like a normal Python function, with parentheses surrounding the arguments.
🌐
Dataquest
dataquest.io › blog › tutorial-lambda-functions-in-python
Tutorial: Lambda Functions in Python
March 6, 2023 - A lambda function is an anonymous function (i.e., defined without a name) that can take any number of arguments but, unlike normal functions, evaluates and returns only one expression.
🌐
Dot Net Perls
dotnetperls.com › lambda-python
Python - lambda Expressions - Dot Net Perls
March 22, 2024 - Lambda expression syntax uses the lambda keyword. The arguments are specified after. A lambda is an object: we can pass it to a def that receives arguments. Part 1 Here 2 lambdas are created.
🌐
Utexas
johnfoster.pge.utexas.edu › numerical-methods-book › PythonIntro_Functions.html
Functions: Argument Types and Lambda Functions
September 8, 2020 - Keyword arguments are defined by assigning a value to the argument, e.g. b=2 in this example. Keyword arguments are optional when calling the function and if not defined, automatically take on the default value that was assigned in the function definition.
🌐
Medium
medium.com › @imdshekhar › python-lam-4554550c0cd8
Python Lambda Function. In Python, developers use lambda… | by Divyanshu Shekhar | Medium
June 19, 2023 - A lambda function in Python can have any number of arguments, including zero arguments. Lambda function with no arguments performs a simple operation without needing any inputs.