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

Gregory Ewing greg.ewing at canterbury.ac.nz
Wed Sep 28 01:33:24 EDT 2016


Peng Yu wrote:
> On Tue, Sep 27, 2016 at 10:01 AM, Chris Angelico <rosuav at gmail.com> wrote:
> 
>>"""In some languages, the variable bindings contained in a closure
>>behave just like any other variables. Alas, in python they are
>>read-only."""
>>
>>This is not true, at least as of Python 3.
> 
> So in Python 2, this is true?

Python 2 doesn't have "nonlocal", so a nested function can't
rebind variables in an intermediate scope (between local and
module-level).

Of course, anything is changeable if you resort to sufficient
amounts of hackery. In this case I believe the hacking level
required involves ctypes.

-- 
Greg



More information about the Python-list mailing list