Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the pit of the ​ lambda parameter

Shulou Source: shulou.com Published: 2022-06-01 01:41:48 10月01日 Update

Today, I will talk to you about the pit of lambda parameters, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

The pit of lambda free parameters

The key functions for sorting and grouping often use lambda, which is more concise, but a novice is easy to fall into:

A = [lambda x: Xampi for i in range (3)] # NO!

For f in a:

Print (f (1))

# you may expect output: 1, 2, 4, 3.

But the actual output: 3, 3, 3, 3, 3. The I that defines the use of lambda is called a free parameter.

It is only when the lambda function is called that the I value is really determined.

It's like printing 2 below. You must be sure.

A = 0

A = 1

A = 2

Def f (a):

Print (a)

The correct way to do this is to convert the free parameter to the default parameter of the lambda function:

A = [lambda x < I > I: X < I > for i in range (3)] # YES!

After reading the above, do you have any further understanding of what the pit of the lambda parameter is? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Parameters freedom content functions output simplicity practices practical novice more knowledge articles industry information information channels channels enter one grouping sorting support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology OPPO Reno Apple Huawei