"Collapsing" a list into a list of changes

Steven Bethard steven.bethard at gmail.com
Mon Feb 7 16:53:17 EST 2005


Francis Girard wrote:
> I see. I personnaly use them frequently to bind an argument of a function with 
> some fixed value. Modifying one of the example in 
> http://mail.python.org/pipermail/python-list/2004-December/257990.html
> I frequently have something like :
> 
> SimpleXMLRPCServer.py:  server.register_1arg-place_function(lambda x: x+2, 
> 'add2')

PEP 309 has already been accepted, so I assume it will appear in Python 2.5:

     http://www.python.org/peps/pep-0309.html

Your code could be written as:

     server.register_1arg-place_function(partial(operator.add, 2))

If your task is actually what you describe above -- "to bind an argument 
of a function with some fixed value"[1] -- then in general, you should 
be able to write this as[2]:

     function_with_fixed_value = partial(function, fixed_value)


Steve

[1] As opposed to binding a name to be used in an _expression_ as you do 
in your example.

[2] The partial function can also be used to fix multiple argument 
values and keyword argument values, if that's necessary for your purposes.



More information about the Python-list mailing list