In [9]: l1 = [0,1,2,3,4,5,6]
In [10]: def a(x,y):
...: return x + y
...:
In [11]: reduce(a,l1) #返回所有参数之和
Out[11]: 21
In [12]: reduce(a,l1,10) #返回所有参数+初始值之和
Out[12]: 31
In [1]: def func1(x): #外层函数
...: def func2(y): #内层函数
...: return y ** x
...: return func2
...:
In [2]: f4 = func1(4)
In [3]: type(f4)
Out[3]: function
In [4]: f4(2)
Out[4]: 16
In [5]: f4(3)
Out[5]: 81
In [6]: def startPos(m,n): #象棋起始位置
...: def newPos(x,y): #象棋新位置
...: return "The old position is (%d,%d),and the new position is (%d,%d)."% (m,n,m+x,n+y)
...: return newPos
...:
In [7]: action = startPos(10,10)
In [8]: action(1,2)
Out[8]: 'The old position is (10,10),and the new position is (11,12).'
In [9]: action = startPos(11,12)
In [10]: action(3,-2)
Out[10]: 'The old position is (11,12),and the new position is (14,10).'