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?

🌐
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.
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
🌐
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.
🌐
AskPython
askpython.com › home › python lambda – anonymous function
Python lambda - Anonymous Function - AskPython
September 5, 2019 - from functools import reduce list_ints = [1, 2, 3, 4, 5, 6] total = reduce(lambda x, y: x + y, list_ints) print(f'Sum of list_ints elements is {total}') ... Yes, we can define a lambda function without any argument.
🌐
Programiz
programiz.com › python-programming › anonymous-function
Python Lambda/ Function (With Examples)
Before you learn about lambdas, make sure to know about Python Functions. We use the lambda keyword instead of def to create a lambda function. Here's the syntax to declare the lambda function: ... Here, we have defined a lambda function and assigned it to the variable named greet.
🌐
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....
🌐
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 ...
Find elsewhere
🌐
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.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-lambda-functions-in-python
Python Anonymous Function – How to Use Lambda Functions
February 15, 2023 - Without assigning a lambda function to a variable, you'd have something like this: print(lambda a,b : a + b) # <function <lambda> at 0x7f757922fb00> The code above simply returns a lambda object in the console.
🌐
Stack Abuse
stackabuse.com › lambda-functions-in-python
Lambda Functions in Python
May 16, 2023 - This creates an anonymous function that receives as input the variables x_1, ..., x_n and returns the evaluated expression(x_1, ..., x_n). ​ The purpose of lambda functions is to be used as parameters for functions that accept functions as parameters, as we did with map() above. Python allows you to assign a lambda function to a variable, but the PEP 8 style guide advises against it.
🌐
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...
🌐
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.
🌐
Educative
educative.io › blog › python-lambda-functions-tutorial
How to use Python Lambda functions: a 5 minute tutorial
A lambda function in Python uses the following basic syntax. As we mentioned before, we use the lambda keyword to create a simple function in a Python expression. ... A lambda expression can have any number of arguments (including none).
🌐
Scaler
scaler.com › home › topics › lambda function in python
Lambda Function in Python (with Examples) - Scaler Topics
February 2, 2024 - Python's lambda function is a ... in functional programming constructs. A lambda function can take any number of arguments including no argument, but they contain only one expression....
🌐
Guru99
guru99.com › home › python › python lambda functions with examples
Python Lambda Functions with EXAMPLES
August 12, 2024 - A Lambda Function in Python programming is an anonymous function or a function having no name. It is a small and restricted function having no more than one line. Just like a normal function, a Lambda function can have multiple arguments with one expression.
Top answer
1 of 2
57

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.

2 of 2
10

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?