Function question

Terry Reedy tejarex at yahoo.com
Mon Apr 8 01:29:10 EDT 2002


"John" <nobody at nobody.com> wrote in message
news:zl9s8.37442$tg4.444316 at vixen.cso.uiuc.edu...
> Let's say there is a function with two arguments.
> def add(a,b): return a+b
>
> What I want to do is:
> I want to get 'a' dynamically..
> But once a value is decided, I want a separate function (say,
new_add) that
> does
> new_add(b) (equals to) add(a,b)

Simple method, if you have source code for func:

>>> def adder(a):
...     def func(b, a=a): return b+a
...     return func
...
#In 2.1x with 'from future import nested scopes'
# and in 2.2, 'a=a' should not be necessary

>>> f = adder(2)
>>> f(5)
7

Terry J. Reedy







More information about the Python-list mailing list