Accessing lambda value passed to function python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am trying to access the value of the lambda functions when i pass the value on the same line as the function is called. the only way i can get the value is to return(f). Is there any way to access the value before this and compare it to other values?

def func(f):
    return(f)

func(lambda x: x*2)(3)
6

Answers

You can t. The (3) is passed to what func() returns, so the func can t access it. This is what happens:

def func(f):
    return f  # parenthesis unnecessary.
func(lambda x: x*2)(3)

Which, when the lambda function is returned, turns to:

(lambda x: x*2)(3)

Which is:

3*2  # 6

This way, you see that the function does not interact with the argument passed to the lambda. You can t access it directly.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/28097975/accessing-lambda-value-passed-to-function-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils