Python関数ネストの例
3195 ワード
Python関数ネストの例について、Pythonの関数ネスト特性.
Pythonでは関数をパラメータとして渡すことができ、他の変数(Javascript、またはC/C++の関数ポインタのようなもの)に値を割り当てることもできます.Javascriptのように、Pythonは関数ネストをサポートし、Javascriptネスト関数の応用モードは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.jbxue.com
>>> def test():
... a = {'name': 'wyj'}
... def f():
... return a['name']
... return a,f
...
>>> a,f = test()
>>> f()
'wyj'
>>> a['name'] = 'ljq'
>>> f()
'ljq'