[py]python函数式编程

函数返回值为数组

def fav():
    return ("lanqiu","zuqiu")
allen = fav()
print(allen)
print("=============")
x,y= fav()
print(x,y)

默认参数 位置参数

def host(host,port=80):
    print("%s:%s"%(host,port))

host("127.0.0.1","8080")
host("8080","127.0.0.1")
host("127.0.0.1")   # 默认参数
host(port = "8080",host = "127.0.0.1")  # 位置参数

不定长参数1-元组

def foo(name,*info):
    print(name)
    print(info)

foo("maotai",11,22,33,44)

不定长参数2-元组,字典 直接传参

def foo(name,*args,**kwargs):
    print(name)
    print(args)
    print(kwargs)

foo("maotai",11,22,33,44,gender="male")
maotai
(11, 22, 33, 44)
{'gender': 'male'}

不定长参数3-3种传参方式


def foo(x,y, *args, **kwargs):
    print(name)
    print(args)
    print(kwargs)

#
foo("maotai", 11, 22, 33, 44, gender="male")
#
foo("maotai", *(11, 22, 33, 44), **{"gender": "male"})

# 
atuple = (1, 2, 3, 4)
adict = {"gender": "male"}
foo("maotai", *atuple, **adict)

匿名函数

lambda arg:表达式

1.求和

add2=lambda x,y=20:x+y

def add(x,y=20): return x+y
def add(x,y=20): return x+y
s = add(10,20)
print(s)

add2=lambda x,y=20:x+y
s2=add2(100,200)
print(s2)

s2=add2(100)
print(s2)

2,显示数组

def showAllTuple(*z):return z

g=lambda *z:z

print(g(1,2,3,4))

简单的闭包

def counter(start_at=0):
    count = [start_at]

    def incr():
        count[0] += 1
        return count[0]

    return incr


count = counter(10)
print(count())
print(count())
print(count())
print(count())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值