Why There Can't Be a Multiline Lambda in Python

Why There Can't Be a Multiline Lambda in Python

Haider Ali Mar-29, 2022 Feb-03, 2022 Python Python Lambda

Why can’t there be a multiline lambda in Python? To understand this question, we need to be familiar with the concept of lambdas in Python. Let’s dive into this guide and check it ourselves.

Why There Is No Multiline Lambda in Python

A lambda in Python is a small anonymous function that can take multiple arguments but can’t have more than one expression.

According to Guido van Rossum (the inventor of Python), it is technically possible to have more arguments, but any solution would become un-Pythonic. So, you can say it’s not about technical reasons; instead, it’s a design choice. Check out his old blog post here.

Take a look at the following code examples.

#trying multiline lambda
z=lambda x:

The above code example will give you an error as you can’t have multiline lambdas in Python.

#lambda function without any argument
z=lambda : "Hello world"


#lambda function with argument
z=lambda x: x*2


#lambda function with Multi arguments
z=lambda x,y: x*y



Hello world

The above code example is correct and simple; thus, it will run without error.

Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.


Related Article - Python Lambda

  • Python Lambda Closure
  • Use Await in a Python Lambda
  • Multiline Lambda Function in Python
  • Lambda Function in Multiple Lines in Python
  • The filter() Method and Lambda Functions in Python