欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python学习-27 匿名函数

程序员文章站 2023-08-14 08:20:39
匿名函数 1. 语法: lanbda x:x+1 运行结果: 2. 运行结果: 3.用法 运行结果: ps:编程的方法论: 面向过程、函数式、面向对象 ......

   匿名函数

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:编程的方法论: 面向过程、函数式、面向对象