Python基础教程 第六章 学习笔记

收集函数

把实际参收集到元组和字典当中

 1 def print_params(*params):
 2      print(params) 
 3 """
 4 print_parasm(1,2,3)
 5 output: (1,2,3)
 6 """
 7 
 8 def print_params_2(**params):
 9     print(params)
10 
11 """
12 print_params_2(x=1, y=2, z=3)
13 output:{'z':3, 'x':1, 'y':2}
14 """

以上的*, **操作也可以用于执行相反的操作

def add(x,y):
    return x + y

params = (1, 2)

add(*params) # output:3

在函数要访问的全局变量和函数内的局部变量重名的时候调用方法

1 #  这种骚操作最好不要使用
2 def combine(parameter):
3     print(parameter + globals()['parameter'])
def multiplier(factor):
    def multiplyByFactor(number):
        return number * factor
    return multiplyByFactor

#  像multiplyByFactor这样存储其所在作用域的函数的函数称为闭包

猜你喜欢

转载自www.cnblogs.com/JokerWu/p/9064145.html
今日推荐