Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argumentless lambdas in Python?

Tags:

python

lambda

Is there a way to code:

def fn():
    return None

as a lambda, in Python?

like image 804
kaspersky Avatar asked Mar 11 '13 14:03

kaspersky


People also ask

What are lambdas in Python?

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

What does lambda X X do?

The whole lambda function lambda x : x * x is assigned to a variable square in order to call it like a named function. The variable name becomes the function name so that We can call it as a regular function, as shown below. The expression does not need to always return a value.

What is the difference between anonymous and lambda function in Python?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.


2 Answers

Yes, the argument list can be omitted:

fn = lambda: None

The production from 5.12. Lambdas is:

lambda_form     ::=  "lambda" [parameter_list]: expression

The square brackets around parameter_list indicate an optional element.

like image 113
ecatmur Avatar answered Oct 01 '22 05:10

ecatmur


You have to do this:

fn = lambda: None
like image 39
marianobianchi Avatar answered Oct 01 '22 06:10

marianobianchi