python 参数_Python函数-参数
生活随笔
收集整理的這篇文章主要介紹了
python 参数_Python函数-参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python中將函數作為參數,區分將參數直接寫成函數名和函數名()的區別。
def fun1(fun):print("---------------------")print("---------------------")print(fun) #執行fun1(fun4)時,fun為函數fun3的返回值xprint('type fun:', type(fun)) #type fun: <class 'str'>fun()#執行fun1(fun4)報錯,執行fun1(fun2)時輸出###### def fun2():print('######')def fun3():x = "!!!!!!!!!!!!!!!!!!!!!"print('hello')return xfun4 = fun3() #傳函數fun3的返回值x print('type fun4:', type(fun4)) #type fun4: <class 'str'> fun1(fun4) print('***************') fun1(fun2) print('type fun2:', type(fun2)) #type fun2: <class 'function'>具體計算例子:
def prepare(a):a = abs(a)return adef add(a, b, f):return f(a) + f(b)sum = add print('type sum:', type(sum)) #type sum: <class 'function'> print(sum(2, -1, prepare)) #3特例(只傳函數名就可以調用函數):sort和sorted函數中的key和cmp(Python3中使用cmp_to_key替代)。
總結
以上是生活随笔為你收集整理的python 参数_Python函数-参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: owasp十大漏洞_OWASP十大网络应
- 下一篇: lambda在python中的意思_Py