Why are functions atomic?
Chris Mellon
arkanes at gmail.com
Tue May 1 11:26:04 EDT 2007
On 1 May 2007 15:17:48 GMT, Duncan Booth <duncan.booth at invalid.invalid> wrote:
> 7stud <bbxx789_05ss at yahoo.com> wrote:
>
> > Does deepcopy work?
>
> It doesn't copy a function.
>
> The easiest way to make a modified copy of a function is to use the 'new'
> module.
>
> >>> def f(x=2): print "x=", x
>
> >>> g = new.function(f.func_code, f.func_globals, 'g', (3,),
> f.func_closure)
> >>> g()
> x= 3
> >>> f()
> x= 2
> --
> http://mail.python.org/mailman/listinfo/python-list
>
The copy module considers functions to be immutable and just returns
the object. This seems pretty clearly wrong to me - functions are
clearly not immutable and it's easy to copy a function using new, as
shown above.
>From copy.py:
def _copy_immutable(x):
return x
for t in (type(None), int, long, float, bool, str, tuple,
frozenset, type, xrange, types.ClassType,
types.BuiltinFunctionType,
types.FunctionType):
d[t] = _copy_immutable
More information about the Python-list
mailing list