Is there a way to change the closure of a python function?

Peter Otten __peter__ at web.de
Tue Sep 27 10:35:16 EDT 2016


Peng Yu wrote:

> Hi, In many other functional language, one can change the closure of a
> function. 

Which are those languages, and how do they work? And is Python actually a 
"functional language"?

> Is it possible in python?

I'm not sure what you mean. Something like 

>>> def f(x):
...     def g(y):
...         return x + y
...     def h(newx):
...         nonlocal x
...         x = newx
...     return g, h
... 
>>> a, b = f(10)
>>> a(42)
52
>>> b(20)
>>> a(42)
62

?
 
> http://ynniv.com/blog/2007/08/closures-in-python.html

A short explanation would have been better than that link.




More information about the Python-list mailing list