>>> defdeco(func): ... defwrapper(): ... print"P1: say something:" ... func() ... print"No zuo no die..." ... return wrapper ... >>> @deco ... defshow(): ... print"i am from Mars" ... >>> show() P1: say something: i am from Mars No zuo no die...
mi = f1(3) type(mi) #返回的是个函数 print mi(2),mi(3),mi(4)
#结果 [root@backup py_basic]# ./py_close.py 82764
很多情景都能用到,比如棋盘的棋子
1 2 3 4 5 6 7 8 9 10 11 12
>>> defstartpos(m,n): ... defnewpos(x,y): ... print"old postion is (%d,%d),new postion is (%d,%d)" % (m,n,m+x,n+y) ... return newpos
>>> action = startpos(10,10) >>> action(1,2) old postion is (10,10),new postion is (11,12) >>> action(3,4) old postion is (10,10),new postion is (13,14) >>> action(-3,-4) old postion is (10,10),new postion is (7,6)