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
🌐
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?

Discussions

Why not real anonymous functions? - Ideas - Discussions on Python.org
For whatever reason I often find myself writing code like this: def returns_a_func(this_var_gets_used): def _inner_func(*args, **kwargs): """Do some stuff in here""" return _inner_func Or like this: def some_useful_func(*args, **kwargs): """Does useful and interesting things, I promise""" ... More on discuss.python.org
🌐 discuss.python.org
4
January 30, 2024
python - Lambda expressions with no parameters in Haskell and / or lambda calculus - Software Engineering Stack Exchange
So, I was expecting that Haskell ... without parameters because the language is already lazy and 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
Why was the "lambda" keyword added for anonymous functions?
Is your question why it's "lambda" in particular? It's because anonymous functions like that are very much inspired by the lambda calculus where you'd write λx.f x for python's lambda x: f(x) and it's just a "pythonization" of that syntax. If you move more into the direction of functional languages you'll find this notation more often: for example lean has λ x, f x instead (as well as I think x => f x in lean 4?) Haskell also has a similar notation - IIRC it's \x -> f x (where the \ is supposed to look kind of like a lambda) or alternatively λx -> f x (I'm not 100% sure on that second one but I think it's valid) More on reddit.com
🌐 r/Python
110
272
November 3, 2023
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
🌐
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.
🌐
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....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Lambda keyword (lambda): Defines an anonymous (inline) function in Python. Argument (x): The input value passed to the lambda function.
Published   2 weeks ago
🌐
Python.org
discuss.python.org › ideas
Why not real anonymous functions? - Ideas - Discussions on Python.org
January 30, 2024 - For whatever reason I often find myself writing code like this: def returns_a_func(this_var_gets_used): def _inner_func(*args, **kwargs): """Do some stuff in here""" return _inner_func Or like this: def some_useful_func(*args, **kwargs): """Does ...
🌐
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.
Find elsewhere
🌐
Tutorial Teacher
tutorialsteacher.com › python › python-lambda-function
Lambda Functions and Anonymous Functions in Python
We can declare a lambda function and call it as an anonymous function, without assigning it to a variable. ... Above, lambda x: x*x defines an anonymous function and call it once by passing arguments in the parenthesis (lambda x: x*x)(5). In ...
🌐
AskPython
askpython.com › home › python lambda – anonymous function
Python lambda - Anonymous Function - AskPython
September 5, 2019 - Yes, we can define a lambda function without any argument. But, it will be useless because there will be nothing to operate on.
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
🌐
Real Python
realpython.com › python-lambda
How to Use Python Lambda Functions – Real Python
December 1, 2023 - The Python lambda function could have been written as lambda x=n: print(x) and have the same result. The Python lambda function is invoked without any argument on line 7, and it uses the default value n set at definition time.
🌐
Python Examples
pythonexamples.org › python-lambda-function-without-arguments
Python Lambda Function without Arguments
In Python, you can define a lambda function without any arguments by using an empty parameter list.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › operators › lambda-expressions
Lambda expressions - Lambda expressions and anonymous functions - C# reference | Microsoft Learn
January 24, 2026 - Use lambda expressions in any code that requires instances of delegate types or expression trees. One example is the argument to the Task.Run(Action) method to pass the code that should be executed in the background.
🌐
Kotlin
kotlinlang.org › docs › lambdas.html
Higher-order functions and lambdas | Kotlin Documentation
Kotlin functions are first-class, which means they can be stored in variables and data structures, and can be passed as arguments to and returned from other higher-order functions. You can perform any operations on functions that are possible for other non-function values. To facilitate this, Kotlin, as a statically typed programming language, uses a family of function types to represent functions, and provides a set of specialized language constructs, such as lambda expressions.
🌐
Medium
ankur-javaarch.medium.com › python-anonymous-lambda-function-588df1fbbdd5
Python Anonymous/Lambda Function. Lambda functions are anonymous, they… | by Ankur Agarwal | Medium
April 20, 2021 - Python Anonymous/Lambda Function Lambda functions are anonymous, they have no names. It’s a one-line function. Syntax of Lambda Function in python lambda arguments: expression Where lambda: is a …
🌐
Codingblocks
codeskiller.codingblocks.com › library › articles › functions-in-python-syntax-with-and-without-arguments
Functions in Python Syntax - With and Without Arguments
Here's an example of a Python function with a return statement: def area_of_rectangle(x, y): """ Returns the area of the rectangle. """ return x * y · This function takes two arguments, x and y, adds them together using the + operator, and returns the result using the return statement. You can call this function and store its output in a variable like this: result = area_of_rectangle(3, 4) print(result) # Output: 12 · And now below is the example of function without return statement
🌐
Medium
medium.com › @nirajan_DataAnalyst › lambda-function-in-python-f8d37b5c7e5e
Lambda Function in Python. A lambda function is an anonymous… | by NIRAJAN JHA | Medium
April 24, 2024 - Lambda Function in Python 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 …
🌐
HowDev
how.dev › answers › how-to-use-a-lambda-function-to-solve-a-problem-in-python
How to use a lambda function to solve a problem in Python
A Python lambda function is a function that has one expression but can take any number of arguments. It is a small anonymous function that is subject to a more restrictive but concise syntax than regular Python functions.