歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python函數嵌套的例子

Python函數嵌套的例子

日期:2017/3/1 9:35:47   编辑:Linux編程

有關Python函數嵌套的實例,Python中的函數嵌套特性。

在Python中函數可以作為參數進行傳遞,而也可以賦值給其他變量(類似Javascript,或者C/C++中的函數指針);
類似Javascript,Python支持函數嵌套,Javascript嵌套函數的應用模式對Python適用;

>>> def multiplier(factor):
... def multiple(number):
... return number * factor
... return multiple
...
>>>
>>> multiplier(3)(3)
9
>>> multiplier(4)(3)

與嵌套函數緊密相關的就是閉包特性,例子:

>>> def test():
... a = {'name': 'wyj'}
... def f():
... return a['name']
... return f
... www.linuxidc.com
>>> def test():
... a = {'name': 'wyj'}
... def f():
... return a['name']
... return a,f
...
>>> a,f = test()
>>> f()
'wyj'
>>> a['name'] = 'ljq'
>>> f()
'ljq'

Python解析xml文檔實例 http://www.linuxidc.com/Linux/2012-02/54760.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved