Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous functions referencing local variables in python

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?

like image 490
ajwood Avatar asked Mar 27 '12 00:03

ajwood


People also ask

How do you access local variables outside a function in Python?

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.

Can an anonymous function be assigned to a variable?

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.

Can I use local variable in another function Python?

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.

How do you call an anonymous function in Python?

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.


2 Answers

Closures in python using functools.partial

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.

Wrapping only some of the args

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.

Example of similar effect using lambda

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.

Tracebacks from exceptions: partial vs lambda vs nested functions

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
like image 64
jdi Avatar answered Sep 27 '22 22:09

jdi


You can achieve this by functools.partial:

f0 = partial(callback, i)
like image 34
tchap Avatar answered Sep 27 '22 22:09

tchap