Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Detailed introduction of Python anonymous functions

Shulou Source: shulou.com Published: 2022-06-02 04:00:26 10月03日 Update

This article mainly explains "the detailed introduction of Python anonymous function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the detailed introduction of Python anonymous functions"!

What is the python function?

Friends who have studied python will know that the function is defined in the way of [def + function name + parameter] in python, that is:

Def function_name (parameters):...

Such a function has a function name, and you only need to call the function:

> function_name (input_parameters)

For the time being, the function defined by def is called "named function"

So what is an anonymous function? As the name implies, such functions do not explicitly define the function name.

Recognize anonymous functions

Anonymous functions do not need to define the function name explicitly, but use [lambda + parameter + expression], that is:

> lambda [arg1 [, arg2,... ArgN]]: expression

That is, lambda is used to represent anonymous functions, and you can pass in multiple parameters, but only one expression.

The following figure shows the uniqueness of anonymous functions

For example, I'm going to write a function to multiply two numbers.

If you write in def:

> def f (XBI y):... Return Xeroy > > f (2pm 3) 6

Write with anonymous functions:

> func = lambda x _ ref y _ r _ r

As you can see, above we assign an anonymous function object to a variable. As long as we call the object directly, we can use the anonymous function:

> func (2) 3) 6

You can also pass a parameter to the anonymous function:

> func_2 = lambda x: X ^ 2 > func_2 (3) 9

Anonymous functions are explained above, and some examples are given to illustrate them. So what are the advantages of anonymous functions?

There is no need to name it, because naming a function is a headache, especially when there are many functions.

You can define it directly where you use it. If you need to modify it, you can find it directly to facilitate the maintenance of the code in the future.

The syntax structure is simple, and you don't need to use the def function name (parameter name): define it this way, directly use the lambda parameter: return the value definition

At this point, I believe you have a deeper understanding of the "detailed introduction of Python anonymous functions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Function parameter way no content object expression learning headache practical deeper famous unique as the name implies the following figure two in use that is code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker MariaDB Shulou Technology OPPO Reno