匿名函数
1.
语法: lanbda x:x+1
def a(x): return x+1 res = a(10) print(res)
运行结果:
11 Process finished with exit code 0
2.
def a(x): return x+1 res = a(10) print(res) print(lambda x:x+1) # 内存地址, X匿名函数 func = lambda x:x+1 # :后就相当于 是return print(func(10))
运行结果:
11 <function <lambda> at 0x00DFA6A8> 11 Process finished with exit code 0
3.用法
# 第一种方法 name = ‘xm‘ def change_name(x): return name +‘_1‘ res = change_name(name) print(res) # 用 lambda a= lambda x:x+‘_2‘ ‘‘‘ 或者: a = lambda x:x+‘_2‘ res = a(name) print(res) ‘‘‘ print(a(name))
运行结果:
xm_1 xm_2 Process finished with exit code 0
ps:编程的方法论: 面向过程、函数式、面向对象
原文地址:https://www.cnblogs.com/liujinjing521/p/11134074.html
时间: 2024-11-10 07:59:31