天池龙珠计划Python训练营——第七天

    有幸参加了阿里云举办的天池龙珠计划Python训练营。收获颇多。

    每天记录一些自己之前的知识盲点,需经常温习。

一、函数

1、函数参数:

3. 可变参数

顾名思义,可变参数就是传入的参数个数是可变的,可以是 0, 1, 2 到任意个,是不定长的参数。

def functionname(arg1, arg2=v, *args):
       "函数_文档字符串"
       function_suite
       return [expression]

*args - 可变参数,可以是从零个到任意个,自动组装成元组。
加了星号(*)的变量名会存放所有未命名的变量参数。
【例子】

def printinfo(arg1, *args):
    print(arg1)
    for var in args:
        print(var)
​
​
printinfo(10)  # 10
printinfo(70, 60, 50)
# 70
# 60
# 50

4. 关键字参数

def functionname(arg1, arg2=v, args, *kw):
       "函数_文档字符串"
       function_suite
       return [expression]

**kw - 关键字参数,可以是从零个到任意个,自动组装成字典。
【例子】

def printinfo(arg1, *args, **kwargs):
    print(arg1)
    print(args)
    print(kwargs)
​
​
printinfo(70, 60, 50)
# 70
# (60, 50)
# {}
printinfo(70, 60, 50, a=1, b=2)
# 70
# (60, 50)
# {'a': 1, 'b': 2}

「可变参数」和「关键字参数」的同异总结如下:

可变参数允许传入零个到任意个参数,它们在函数调用时自动组装为一个元组 (tuple)。
关键字参数允许传入零个到任意个参数,它们在函数内部自动组装为一个字典 (dict)。

2、变量作用域:

闭包

是函数式编程的一个重要的语法结构,是一种特殊的内嵌函数。
如果在一个内部函数里对外层非全局作用域的变量进行引用,那么内部函数就被认为是闭包。
通过闭包可以访问外层非全局作用域的变量,这个作用域称为 闭包作用域。
【例子】

def funX(x):
    def funY(y):
        return x * y
​
    return funY
​
​
i = funX(8)
print(type(i))  # <class 'function'>
print(i(5))  # 40

【例子】闭包的返回值通常是函数。

def make_counter(init):
    counter = [init]
​
    def inc(): counter[0] += 1
​
    def dec(): counter[0] -= 1
​
    def get(): return counter[0]
​
    def reset(): counter[0] = init
​
    return inc, dec, get, reset
​
​
inc, dec, get, reset = make_counter(0)
inc()
inc()
inc()
print(get())  # 3
dec()
print(get())  # 2
reset()
print(get())  # 0


【例子】 如果要修改闭包作用域中的变量则需要 nonlocal 关键字

def outer():
    num = 10
​
    def inner():
        nonlocal num  # nonlocal关键字声明
        num = 100
        print(num)
​
    inner()
    print(num)
​
​
outer()
​
# 100
# 100

二、Lambda表达式

1、匿名函数的应用

函数式编程 是指代码中每一块都是不可变的,都由纯函数的形式组成。这里的纯函数,是指函数本身相互独立、互不影响,对于相同的输入,总会有相同的输出,没有任何副作用。

【例子】非函数式编程

def f(x):
    for i in range(0, len(x)):
        x[i] += 10
    return x
​
​
x = [1, 2, 3]
f(x)
print(x)
# [11, 12, 13]

【例子】函数式编程

def f(x):
    y = []
    for item in x:
        y.append(item + 10)
    return y
​
​
x = [1, 2, 3]
f(x)
print(x)
# [1, 2, 3]

匿名函数 常常应用于函数式编程的高阶函数 (high-order function)中,主要有两种形式:

参数是函数 (filter, map)
返回值是函数 (closure)
如,在 filter和map函数中的应用:

filter(function, iterable) 过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换。
【例子】

odd = lambda x: x % 2 == 1
templist = filter(odd, [1, 2, 3, 4, 5, 6, 7, 8, 9])
print(list(templist))  # [1, 3, 5, 7, 9]

map(function, *iterables) 根据提供的函数对指定序列做映射。
【例子】

m1 = map(lambda x: x ** 2, [1, 2, 3, 4, 5])
print(list(m1))  
# [1, 4, 9, 16, 25]
​
m2 = map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
print(list(m2))  
# [3, 7, 11, 15, 19]

除了 Python 这些内置函数,我们也可以自己定义高阶函数。

【例子】

def apply_to_list(fun, some_list):
    return fun(some_list)
​
lst = [1, 2, 3, 4, 5]
print(apply_to_list(sum, lst))
# 15
​
print(apply_to_list(len, lst))
# 5
​
print(apply_to_list(lambda x: sum(x) / len(x), lst))
# 3.0

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值