Modifying func_closure

Robert Brewer fumanchu at
Thu Jul 8 18:38:14 CEST 2004

Jacek Generowicz wrote:
> Python's closures are read-only. But is there a way of hacking one's
> way around this restriction?
> The funct_closure attribute is read-only, so I can't rebind it. It's a
> tuple, so I can't mutate it. Even if I could, I can't instantiate cell
> ojects. The cell objects don't seem to have any interesting attributes
> that one might use to mutate the contents.
> Looks hopeless, but maybe someone knows better.

If I understand your requirements correctly, I would use new.function(),
passing it the closure you want.

return new.function(co, f.func_globals, newname, f.func_defaults,

...ah, I see now (looking at the LibRef), that last argument isn't
documented. But it's there; trust me. :)

Robert Brewer
Amor Ministries
fumanchu at

More information about the Python-list mailing list