How can I define anonymous functions in python, where the bahaviour should depend on the value of a local variable at definiton-time, and also accept arguments
Example:
def callback(val1, val2):
print "{0} {1}".format(val1, val2)
i = 0
f0 = lambda x: callback(i, x)
i = 1
f1 = lambda x: callback(i, x)
f0(8) # prints "1, 8: but I'd like "0, 8" (value of 'i' when f0 was defined)
f1(8) # prints "1, 8"
Is something like this possible without wrapping my callback in its own a class?
Using the global statement If you want to assign a value to a name defined outside the function, then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.
An anonymous function in javascript is not accessible after its initial creation. Therefore, we need to assign it to a variable, so that we can use its value later. They are always invoked (called) using the variable name. Also, we create anonymous functions in JavaScript, where we want to use functions as values.
In Python or any other programming languages, the definition of local variables remains the same, which is “A variable declared inside the function is called local function”. We can access a local variable inside but not outside the function.
While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.
from functools import partial
i = 0
f0 = partial(callback, i)
i = 1
f1 = partial(callback, i)
f0()
# 0
f1()
# 1
partial
is like a lambda but wraps the value at that moment into the arg. Not evaluating it when its called.
Yes partial will allow you to wrap any number of the arguments, and the remaining args and kwargs can then be passed to the resulting partial object so that it acts like it was calling the original wrapped function...
def callback(val1, val2):
print "{0} {1}".format(val1, val2)
i = 0
x = 8
f0 = partial(callback, i)
f0(x)
# 0 8
Essentially you have wrapped callback(val1, val2)
into callback(val2)
with val1
being included as a closure already.
In case you really want to see how to do this with a lambda closure, you can see why it gets ugly and partial is preferred...
f0 = (lambda val1: lambda val2: callback(val1, val2))(i)
You have to wrap the scope variable into an outer function scope, and then reference that scope in the inner lambda function. Yuk.
With the influx of other answers, I thought I would outline one more reason to use partial as opposed to lambda, or a inner/outer function closure. Keep in mind I mean a function closure. functools.partial fixes the traceback you will get when your wrapped function raises an exception...
Consider this version that will raise a division by zero:
def callback(val1, val2):
return val1 / val2
Normal outter/inner closure
def wrapper(fn, val1):
def wrapped(val2):
return fn(val1, val2)
return wrapped
f0 = wrapper(callback, i)
f0(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in wrapped
File "<stdin>", line 2, in callback
ZeroDivisionError: integer division or modulo by zero
lambda closure
f0 = (lambda val1: lambda val2: callback(val1, val2))(i)
f0(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
File "<stdin>", line 2, in callback
ZeroDivisionError: integer division or modulo by zero
And now for functools.partial
f0 = partial(callback, i)
f0(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in callback
ZeroDivisionError: integer division or modulo by zero
You can achieve this by functools.partial:
f0 = partial(callback, i)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With