Confused about lambda expression in python

I understand a normal lambda expression like

g = lambda x: x**2 

However, for some complex ones, I'm a little confused. For instance:

 for split in ['train', 'test']: sets = (lambda split=split: newspaper(split, newspaper_devkit_path)) def get_imdb(): return sets() 

Where newspaper is a function. I was wondering what sets actually are and why the get_imdb function can return sets()

Thank you for your help!

Added: Codes are really from here factory.py

+5
source share
3 answers

sets is assigned a lambda, which in fact should not accept the input that you see by the way it is called. Typically, Lambdas behave like normal functions and therefore can be assigned to variables like g or sets . The definition of sets is surrounded by an additional set of parentheses for no apparent reason. You can ignore these external parades.

Lambdas can have the same types of positional, keyword, and default arguments that can perform common functions. Lambda sets has a default split option. This is a common idiom, ensuring that sets at each iteration of the loop gets a split value corresponding to that iteration, and not just one of the last iteration in all cases.

Without the default parameter, split will be evaluated in lambda based on the namespace at the time it is called. Once the loop completes, split in the namespace of the external functions will be only the last value it had for the loop.

The default parameters are evaluated immediately after the creation of the functional object. This means that the default value of split will be where it is in the iteration of the loop that creates it.

Your example is a bit misleading, as it discards all actual sets , except the last, which makes the default parameter for lambda pointless. Here is an example illustrating what happens if you save all lambdas. First specify the default parameter:

  sets = []
 for split in ['train', 'test']:
     sets.append (lambda split = split: split)
 print ([fn () for fn in sets])

I shortened the lambdas just to return their input parameter for illustration purposes. In this example, ['train', 'test'] will be printed, as expected.

If you do the same without the default parameter, there will be ['test', 'test'] instead:

  sets = []
 for split in ['train', 'test']:
     sets.append (lambda: split)
 print ([fn () for fn in sets])

This is because 'test' is a split value when all lambdas get evaluated.

+2
source

Lambda function:

 func = lambda x: x**2 

can be rewritten almost equivalently:

 def func(x): return x**2 

Using any method, you can call the function as follows:

 func(4) 

In your example

 sets = lambda split=split: newspaper(split, newspaper_devkit_path) 

can be rewritten:

 def sets(split=split): return newspaper(split, newspaper_devkit_path) 

and therefore can be called:

 sets() 

When you write the following:

 def get_imdb(): return sets() 

you define "closure". The reference to the sets function is stored within get_imdb so that it can be called later, where get_imdb is get_imdb .

0
source

You may be confused about the split=split . This has the same meaning as in a regular function: the separation on the left is the parameter of the lambda function, and the separation on the right is the default value, which takes up the left separation when the value is not set. In this case, the default is the split variable defined in the for loop.

So, answering your first question (what is sets ?):

sets is the variable to which the anonymous function (or lambda function) is assigned. This allows you to reference the lambda function and use it through the sets variable.

To your second question (why can you return sets() ?), I answer:

Since sets is a variable that acts like a function, adding brackets after calling the lambda function. Since no parameters are specified, the split parameter takes the value 'test' , which is the last value that the split loop variable takes. It is worth noting here that since sets not defined inside the get_imdb function, the interpreter looks for the definition of sets outside the scope of get_imdb (and finds the one that references the lambda function).

0
source

Source: https://habr.com/ru/post/1272059/


All Articles