kaspersky
kaspersky

Reputation: 4117

Argumentless lambdas in Python?

Is there a way to code:

def fn():
    return None

as a lambda, in Python?

Upvotes: 5

Views: 598

Answers (3)

dev_hero
dev_hero

Reputation: 350

to be sure that works even with positional and kewyword arguments:

fn = lambda *args, **kwargs: None

Upvotes: 0

marianobianchi
marianobianchi

Reputation: 8488

You have to do this:

fn = lambda: None

Upvotes: 1

ecatmur
ecatmur

Reputation: 157414

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.

Upvotes: 12

Related Questions