help with map function

Duncan Booth duncan.booth at invalid.invalid
Sat Oct 23 10:58:48 CEST 2004


>> I don't have any experience working with *args, **kw arguments.
>> From what I've read, *args allows for an unknown number of
>> arguments to passed into a function and **kw allows for an
>> unknown number of key-value pairs to passed into a function.
>> But from what I understand, both are optional.  This shouldn't
>> change the how you call apply_each.
> I spoke too soon. If the function being called doesn't provide for
> *args, or **kw then a TypeError exception will be thrown.

The original apply_each expected you to use it something like this:

def fn1(a,b,c): ... whatever ...
def fn2(a,b,c): ... whatever ...

result = apply_each([fn1, fn2], [1, 2, 3])

A revised version:

def apply_each(fns, *args, **kw):
   return [ fn(*args, **kw) for fn in fns ]

could be called in a variety of ways:

result = apply_each([fn1, fn2], 1, 2, 3)
result = apply_each([fn1, fn2], *[1, 2, 3])
result = apply_each([fn1, fn2], a=1, c=3, b=2)
result = apply_each([fn1, fn2], **{'a': 1, 'c': 3, 'b': 2})

The functions actually being called don't need to know anything about how 
they are invoked, they just need to get a suitable combination of 
positional and/or keyword arguments.

More information about the Python-list mailing list