文字笑话,龙书浩,沾化论坛
在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
...
>>> def test():
... a = {'name': 'wyj'}
... def f():
... return a['name']
... return a,f
...
>>> a,f = test()
>>> f()
'wyj'
>>> a['name'] = 'ljq'
>>> f()
'ljq'
例子3
#! /usr/bin/python
def yao1(a=1,b=32):
def yao2(c=54):
def yao3(d=3):
def yao4(e=10):
return a+b+c+d+e
return yao4
return yao3
return yao2
print yao1()()()()
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
python如何查看网页代码
用python查看网页代码的方法:1、使用“import”导入requests包import requests2、使用requests包的get()函数通过网页...
[阅读全文]
-
-
python如何保存文本文件
python保存文本文件的方法:使用python内置的open()类可以打开文本文件,向文件里面写入数据可以用write()函数,写完之后,使用close()函...
[阅读全文]
-
python如何编写win程序
python可以编写win程序。win程序的格式是exe,下面我们就来看一下使用python编写exe程序的方法。编写好python程序后py2exe模块即可将...
[阅读全文]
-
-
-
-
-
-
网友评论