Modify bound variables of a closure in Python - python

Is there any way to modify the bound value of one of the variables inside a closure? Look at the example to understand it better.
def foo():
var_a = 2
var_b = 3
def _closure(x):
return var_a + var_b + x
return _closure
localClosure = foo()
# Local closure is now "return 2 + 3 + x"
a = localClosure(1) # 2 + 3 + 1 == 6
# DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0
# ...but what magic? Is this even possible?
# Local closure is now "return 0 + 3 + x"
b = localClosure(1) # 0 + 3 +1 == 4

It is quite possible in python 3 thanks to the magic of nonlocal.
def foo():
var_a = 2
var_b = 3
def _closure(x, magic = None):
nonlocal var_a
if magic is not None:
var_a = magic
return var_a + var_b + x
return _closure
localClosure = foo()
# Local closure is now "return 2 + 3 + x"
a = localClosure(1) # 2 + 3 + 1 == 6
print(a)
# DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0
localClosure(0, 0)
# Local closure is now "return 0 + 3 + x"
b = localClosure(1) # 0 + 3 +1 == 4
print(b)

I don't think there is any way to do that in Python. When the closure is defined, the current state of variables in the enclosing scope is captured and no longer has a directly referenceable name (from outside the closure). If you were to call foo() again, the new closure would have a different set of variables from the enclosing scope.
In your simple example, you might be better off using a class:
class foo:
def __init__(self):
self.var_a = 2
self.var_b = 3
def __call__(self, x):
return self.var_a + self.var_b + x
localClosure = foo()
# Local closure is now "return 2 + 3 + x"
a = localClosure(1) # 2 + 3 + 1 == 6
# DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0
# ...but what magic? Is this even possible?
localClosure.var_a = 0
# Local closure is now "return 0 + 3 + x"
b = localClosure(1) # 0 + 3 +1 == 4
If you do use this technique I would no longer use the name localClosure because it is no longer actually a closure. However, it works the same as one.

I've found an alternate answer answer to Greg's, slightly less verbose because it uses Python 2.1's custom function attributes (which conveniently enough can be accessed from inside their own function).
def foo():
var_b = 3
def _closure(x):
return _closure.var_a + var_b + x
_closure.func_dict['var_a'] = 2
return _closure
localClosure = foo()
# Local closure is now "return 2 + 3 + x"
a = localClosure(1) # 2 + 3 + 1 == 6
# DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0
# ...but what magic? Is this even possible?
# apparently, it is
localClosure.var_a = 0
# Local closure is now "return 0 + 3 + x"
b = localClosure(1) # 0 + 3 +1 == 4
Thought I'd post it for completeness. Cheers anyways.

We've done the following. I think it's simpler than other solutions here.
class State:
pass
def foo():
st = State()
st.var_a = 2
st.var_b = 3
def _closure(x):
return st.var_a + st.var_b + x
def _set_a(a):
st.var_a = a
return _closure, _set_a
localClosure, localSetA = foo()
# Local closure is now "return 2 + 3 + x"
a = localClosure(1) # 2 + 3 + 1 == 6
# DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0
localSetA(0)
# Local closure is now "return 0 + 3 + x"
b = localClosure(1) # 0 + 3 +1 == 4
print a, b

I worked around a similar limitation by using one-item lists instead of a plain variable. It's ugly but it works because modifying a list item doesn't get treated as a binding operation by the interpreter.
For example:
def my_function()
max_value = [0]
def callback (data)
if (data.val > max_value[0]):
max_value[0] = data.val
# more code here
# . . .
results = some_function (callback)
store_max (max_value[0])

Question
Is there any way to modify the bound value of one of the variables inside a closure?
TLDR
Yes, this is possible starting in Python 3.7.0 alpha 1:
localClosure.__closure__[0].cell_contents = 0
Details
In Python, a closure remembers the variables from the scope in which it was defined by using a special __closure__ attribute. The __closure__ attribute is a tuple of cell objects representing the variables from the outer scope, and the values of those variables are stored in the cell_contents attribute of each cell.
Given the code from the question, this can be seen by running the following:
# print the list of cells
print(localClosure.__closure__)
# (<cell at 0x7f941ca27a00: int object at 0x7f941a621950>, <cell at 0x7f941ca27eb0: int object at 0x7f941a621970>)
# print the values in the cells
print(', '.join(str(cell.cell_contents) for cell in localClosure.__closure__))
# 2, 3
# print the value in the first cell (var_a)
print(localClosure.__closure__[0].cell_contents)
# 2
The cell_contents attribute of the cell objects first became writable with bpo-30486 which was first included in Python 3.7.0 alpha 1
Complete working example:
def foo():
var_a = 2
var_b = 3
def _closure(x):
return var_a + var_b + x
return _closure
localClosure = foo()
# Local closure is now "return 2 + 3 + x"
a = localClosure(1) # 2 + 3 + 1 == 6
# DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0
# ...but what magic? Is this even possible?
# the magic
# this changes the value in the cell representing var_a to be 0
localClosure.__closure__[0].cell_contents = 0
# Local closure is now "return 0 + 3 + x"
b = localClosure(1) # 0 + 3 + 1 == 4

slightly different from what was asked, but you could do:
def f():
a = 1
b = 2
def g(x, a=a, b=b):
return a + b + x
return g
h = f()
print(h(0))
print(h(0,2,3))
print(h(0))
and make the closure the default, to be overridden when needed.

Maybe there's a further approach (even if it seems to be some years too late for my proposal :-)
def foo():
def _closure(x):
return _closure.var_a + _closure.var_b + x
_closure.var_a = 2
_closure.var_b = 3
return _closure
localClosure = foo()
# Local closure is now "return 2 + 3 + x"
a = localClosure(1) # 2 + 3 + 1 == 6
print(a)
# DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0
# ...but what magic? Is this even possible?
localClosure.var_a = 0
# Local closure is now "return 0 + 3 + x"
b = localClosure(1) # 0 + 3 +1 == 4
print(b)
From my point of view the class solution proposed is easier to read. But if you try to modiy a free variable inside a decorator this solution might come in handy: In comparison to a class based solution it's easier to work with functools.wraps to preserve the meta data of the decorated function.

Why not make var_a and var_b arguments of the function foo?
def foo(var_a = 2, var_b = 3):
def _closure(x):
return var_a + var_b + x
return _closure
localClosure = foo() # uses default arguments 2, 3
print localClosure(1) # 2 + 3 + 1 = 6
localClosure = foo(0, 3)
print localClosure(1) # 0 + 3 + 1 = 4

def foo():
var_a = 2
var_b = 3
def _closure(x):
return var_a + var_b + x
return _closure
def bar():
var_a = [2]
var_b = [3]
def _closure(x):
return var_a[0] + var_b[0] + x
def _magic(y):
var_a[0] = y
return _closure, _magic
localClosureFoo = foo()
a = localClosureFoo(1)
print a
localClosureBar, localClosureBarMAGIC = bar()
b = localClosureBar(1)
print b
localClosureBarMAGIC(0)
b = localClosureBar(1)
print b

Related

Python local variable defined in an enclosing scope referenced before assignment in multiple functions

I have the following code:
class MyFirstClass:
def myDef(self):
global x, y
x = 'some_str'
y = 0
class MySecondClass:
def myOtherDef(self):
for i in range(10):
y += 1
if y % 2 == 0:
y = 0
x = 'new_str'
However, y += 1 errors with local variable 'y' defined in an enclosing scope on line 4 referenced before assignment
How can I re-assign variable y in the new class?
There are 2 issues with this. The first is that you need to define x and y as members of MyFirstClass, not as globals in a method. You won't be able to access them that way. The second is that you need an instance of MyFirstClass in MySecondClass:
class MyFirstClass:
x = 'some_str'
y = 0
class MySecondClass:
myFirstClass = MyFirstClass()
def myOtherDef(self):
for i in range(10):
self.myFirstClass.y += 1
if self.myFirstClass.y % 2 == 0:
self.myFirstClass.y = 0
self.myFirstClass.x = 'new_str'
print(self.myFirstClass.x)
mySecondClass = MySecondClass()
mySecondClass.myOtherDef()
Output:
new_str
new_str
new_str
new_str
new_str

How can I assign zero to missing variable in function

First of all, thank you for reading it. I am new to Python and learning something new every day.
I wrote a function where inputs are 4 variables and output are 4 variables.
My problem is variables are getting defined somewhere else and it's calling the script. Now, what if the user only defines 3 variables in that case I automatically want a 4th variable to get assigned zero value. I also included another code that I am trying to use for the same purpose.
volsum = vol1 + vol2 + vol3 + vol4
if vol1n == missing:
vol1n = 0
else:
vol1n=vol1/volsum
if vol2n == missing:
vol2n = 0
else:
vol2n=vol2/volsum
if vol3n = missing:
vol3n = 0
else:
vol3n=vol3/volsum
if vol4n == missing:
vol4n = 0
else:
vol4n=vol4/volsum
or maybe using a function
def vol(vol1,vol2,vol3,vol4):
volsum = vol1 + vol2
vol1n=vol1/volsum
vol2n=vol2/volsum
vol3n=vol3/volsum
vol4n=vol4/volsum
return vol1n, vol2n,vol3n,vol4n
Try Assigning Values when you Define the Fucntion:
def vol(vol1 = 0,vol2 = 0,vol3 = 0,vol4 = 0):
volsum = vol1 + vol2
vol1n = vol1 / volsum
vol2n = vol2 / volsum
vol3n = vol3 / volsum
vol4n = vol4 / volsum
return vol1n, vol2n,vol3n,vol4n
Now suppose you want to skip vol2:
variable_assigned = vol(vol1 = 5,vol3 = 2,vol4 = 5)
If you require a One-Line (I love creating one):
#defining vol()
def vol(*kwargs): return [i/sum(kwargs) for i in kwargs]
# using vol()
#example
vol (1,2,3)
#returns
[0.16666666666666666, 0.3333333333333333, 0.5]
# so to assign variables,
#if you give 3 values,
vol1n, vol2n, vol3n = vol(1,2,3)
# if 4,
vol1n, vol2n, vol3n, vol4n = vol(1,2,3,5)
A little Fun (Not recommended):
Lets say your vol1 =1, vol2 = 5 vol3 = 3 ...voln = 10
So let:
vol = [ 1, 5, 3, ... n values, 10]
Now if you run the following code:
for j in range(len(vol)): exec(f'vol{j+1}n = [i/sum(vol) for i in vol][j]')
This code will automatically create your vol1n, vol2n, ... volnn variables automatically.
Rather than a 0, could it be forgotten? Personally, I don't like typing things out when they don't need to be. Try the *args feature out.
def vol_func(*args):
volumesum = sum(args)
vals = []
for i in range(len(args)):
vals.append(args[i] / volumesum)
return vals
volsumlst = vol_func(2.3, 4)
You can check if a variable is defined or not using 'locals' function. Alternatively if variables are defined globally, then you should use 'globals' function.
if 'vol1n' not in locals():
vol1n = 0
else:
vol1n=vol1/volsum
if 'vol2n' not in locals():
vol2n = 0
else:
vol2n=vol2/volsum
if 'vol3n' not in locals():
vol3n = 0
else:
vol3n=vol3/volsum
if 'vol4n' not in locals():
vol4n = 0
else:
vol4n=vol4/volsum

New Environment after Calling a Function inside another Function

I have this program:
a = 0
def f(c):
a = c
return a + g(a)
def g(b):
return a + b
f(3) #returns 6
How does it return the value 6? So far I know how binding environments are created when the python interpreter evaluates a variable and a procedure. Also I know the details how a new environment is made when a simple function (in the sense it doesn't call a new function) is called. After the call g(a) inside f, is there a new environment created exclusive for g that is one level below the global environment, or one level below the environment of f?
It's exactly the same as:
a = 0
def f(c):
var = c
return var + g(var)
def g(b):
return a + b
f(3)
And the same as:
def f(c):
var = c
return var + g(var)
def g(b):
return 0 + b
f(3)
And even more simplified:
def f(c):
return c + g(c)
def g(b):
return b
f(3)
When only reading the values of global variables, you can use them inside a function just fine. But if you try to assign to a global variable you need to use the global keyword, or otherwise you shadow the global with the local variable:
x= 123
def foo():
x = 999 # shadows global x
print(x) # 999
print(x) # 123
x = 123
def foo():
global x
x = 999 # changes global x
print(x) # 999
print(x) # 999

return a mathematical function as string

suppose I have a function:
def func(t):
a=5;b=-7;c=4;d=2
return a*t**3+b*t**2+c*t+d
beside returning the value of the function, I am also trying to get the function literally, as, when called, I will get:
a*t**3 +b*t**2+c*t +d = <actual mathematical value>
My final goal is to get it correct as a LaTeX expression to write the statement in matplotlib.
Any help?
UPDATE Dear all, Thanks for your reply. But I just don't want to write the function once again, as you have shown, is some way like:
return "a*t**3+b*t**2+c*t+d = " + str(a*t**3+b*t**2+c*t+d)
(I can easier do that in plt.txt directly, right?)
I just want to transform the function copied literally:
def func(t):
a=.05;b=-.07;c=.04;d=.02
return a*t**3+b*t**2+c*t+d
def strf():
# return(r"$a*t**3+b*t**2+c*t+d$")
return (str(func))
# expecting this to give the output
# r"$a*t**3+b*t**2+c*t+d$"
Is this what you mean?
def func(t):
a = 5
b = -7
c = 4
d = 2
return "a*t**3+b*t**2+c*t+d = " + str(a*t**3+b*t**2+c*t+d)
def func(t):
a = 5
b = -7
c = 4
d = 2
exp = "a*t**3+b*t**2+c*t+d"
return f"{exp} = {eval(exp)}"
I think this is it
You can use the f-string:
def func(t):
a = 5
b = -7
c = 4
d = 2
result = a * t**3 + b * t**2 + c * t + d
return result, f'a * t**3 + b * t**2 + c * t + d = {result}'
res, str_ = func(5)
print(res)
print(str_)
Output:
472
a * t**3 + b * t**2 + c * t + d = 472
I don‘t recommend you to use the function eval() as in another solutions.

How to pass additional parameters (besides arguments) to a function?

I need to write a function (say fun1) that has one argument, because it will be used in other function (fun2). The latter requires a function with a single argument. However, I need to pass other parameters to function fun1. How can I do this in Python without using global variables? Or this is the only way?
Addition: If it is important, fun2 is some optimization function from scipy.optimize. Below is an example of passing additional parameter c to function fun1 using global. In the first call, function fun2 takes fun1 as x+1, but in the second call, fun1 is x+2. I would like to make similar, but without using global. Hopefully, the example clarifies the question. (The example is changed).
def fun1(x) :
global c
return x + c
def fun2(f1, x) :
return f1(x)
# main program
global c
x0= 1
c= 1; y= fun2(fun1, x0); print(y) # gives 2
c= 2; y= fun2(fun1, x0); print(y) # gives 3
If I've understood your question correctly, there are quite a number of ways to do what you want and avoid using global variables. Here they are.
Given:
x0 = 1
def fun2(f1, x):
return f1(x)
All of these techniques accomplish your goal:
#### #0 -- function attributes
def fun1(x):
return x + fun1.c
fun1.c = 1; y = fun2(fun1, x0); print(y) # --> 2
fun1.c = 2; y = fun2(fun1, x0); print(y) # --> 3
#### #1 -- closure
def fun1(c):
def wrapper(x):
return x + c
return wrapper
y = fun2(fun1(c=1), x0); print(y) # --> 2
y = fun2(fun1(c=2), x0); print(y) # --> 3
#### #2 -- functools.partial object
from functools import partial
def fun1(x, c):
return x + c
y = fun2(partial(fun1, c=1), x0); print(y) # --> 2
y = fun2(partial(fun1, c=2), x0); print(y) # --> 3
#### #3 -- function object (functor)
class Fun1(object):
def __init__(self, c):
self.c = c
def __call__(self, x):
return x + self.c
y = fun2(Fun1(c=1), x0); print(y) # --> 2
y = fun2(Fun1(c=2), x0); print(y) # --> 3
#### #4 -- function decorator
def fun1(x, c):
return x + c
def decorate(c):
def wrapper(f):
def wrapped(x):
return f(x, c)
return wrapped
return wrapper
y = fun2(decorate(c=1)(fun1), x0); print(y) # --> 2
y = fun2(decorate(c=2)(fun1), x0); print(y) # --> 3
Note that writing c= arguments wasn't always strictly required in the calls -- I just put it in all of the usage examples for consistency and because it makes it clearer how it's being passed.
The fact that that function can be called even without those other parameters suggests, that they are optional and have some default value. So you should use default arguments.
def fun1(foo, bar='baz'):
# do something
This way you can call function fun1('hi') and bar will default to 'baz'. You can also call it fun1('hi', 15).
If they don't have any reasonable default, you can use None as the default value instead.
def fun1(foo, bar=None):
if bar is None:
# `bar` argument was not provided
else:
# it was provided
What you are looking for is a method in a class.
you define a class, with a method fun1 and an instance variable c. it is accessed from anywhere using the . notation:
class A:
def fun1(self, x):
return x + self.c
Let's define fun2, for the example:
def fun2(f, p):
return f(p)
We can now use a.c it like you did with the global varaible c:
>>> a = A() # create an instance and initialize it
>>> # "self.c" is undefined yet
>>>
>>> a.c = 1 # "self.c" will be 1
>>> fun2(a.fun1, 1)
2
>>> a.c = 2 # now "self.c" will be 2
>>> fun2(a.fun1, 1) # same arguments, different result
3
Here you can learn more about classes.
Just add the extra parameters with default values:
def fun1(param1, param2=None, param3=None):
...
Then you can call fun1 from fun2 like this:
def fun2():
something = fun1(42)
And from somewhere else you can call it like this:
fun1(42, param2=60)
You may use the decorators to pass it
the very decorators:
def jwt_or_redirect(fn):
#wraps(fn)
def decorator(*args, **kwargs):
...
return fn(*args, **kwargs)
return decorator
def jwt_refresh(fn):
#wraps(fn)
def decorator(*args, **kwargs):
...
new_kwargs = {'refreshed_jwt': 'xxxxx-xxxxxx'}
new_kwargs.update(kwargs)
return fn(*args, **new_kwargs)
return decorator
and the final function:
#jwt_or_redirect
#jwt_refresh
def home_page(*args, **kwargs):
return kwargs['refreched_jwt']

Categories

Resources