Why are functions atomic?

Laurent Pointal laurent.pointal at wanadoo.fr
Tue May 1 10:24:30 EDT 2007


Michael wrote:

> Why are functions atomic?  (I.e. they are not copied.)
> 
> For example, I would like to make a copy of a function so I can change
> the default values:
> 
>>>> from copy import copy
>>>> f = lambda x: x
>>>> f.func_defaults = (1,)
>>>> g = copy(f)
>>>> g.func_defaults = (2,)
>>>> f(),g()
> (2, 2)
> 
> I would like the following behaviour:
> 
>>>> f(),g()
> (1,2)
> 
> I know I could use a 'functor' defining __call__ and using member
> variables, but this is more complicated and quite a bit slower.  (I
> also know that I can use new.function to create a new copy, but I
> would like to know the rational behind the decision to make functions
> atomic before I shoot myself in the foot;-)
> 
> Thanks,
> Michael.

This dont make functions copiable but may resolve your default arguments
problem. Under Python 2.5, see functools.partial().

http://docs.python.org/lib/partial-objects.html




More information about the Python-list mailing list