I think, that variant
newlist = map(f, range(10)) with:
def f(x):
global sum
sum+=x
return sum
that i saw in Kay Schluehr post is quite not bad, except that I think it is better to use the existing statment in rather then with. Thus we don't need to introduce a new key-word.
Here are some more examples of this construction:
#===================
a = map( f, [1,2,3] ) in:
def f(n):
print n
return n+1
#====================
def add_funcs( f1, f2, arg ):
return f1( arg ) + f2( arg )
sum = add_funcs( f1, f2, 5 ) in:
def f1(n):
return n+1
def f2(n):
return n**n
#======================
# Or even thus:
sum = add_funcs( f1, f2, 5 ) in:
def f1(n):
return n+1
def f2(n):
return n**n
def add_funcs( f1, f2, arg ):
return f1( arg ) + f2( arg )
#=======================
map( f, range(10) ) in:
def f(n):
print n,'*',n,'=',n*n
#=======================
The advantages of such approach are:
1) Functions are anonimous, that is they are not visible in outside scope.
2) Thus can be defined more then one anonimous function in one construction.
3) No new key-words are introduced in language
The fact, that we nevertheless have to name our anonimous functions doesn't seem unreasonable, because in list comprehensions we have also name intermediate element.
Disadvantages:
I don't see ones... Or am I wrong?
What you think?