标签:this 部分 元组 函数名 enc 分享 它的 glob else
def set_passline(passline): def cmp(val): if val >= passerine: print(‘Pass‘) else: print(‘failed‘) return cmp f_100 = set_passline(60) f_100(89) f_150 = set_passline(96) f_150(89)
def my_sum(*arg): print(‘my_sum‘) return sum(arg) def my_average(*arg): print(‘my_average‘) return sum(arg) / len(arg) def dec(func): # 闭包 def in_dec(*arg): print(‘in_dec()=‘, arg) # 将相同的处理部门进行封装 if len(arg) == 0: return 0 for val in arg: if not isinstance(val, int): return 0 # 处理完相同部门,返回不同函数的特定处理 return func(*arg) return in_dec my_sum = dec(my_sum) print(my_sum(1, 2, 3, 4, 5, 6)) print(my_sum(1, 2, 3, 4, 5, ‘6‘)) my_average = dec(my_average) print(my_average(1, 2, 3, 4, 5, 6)) print(my_average(1, 2, 3, 4, 5, ‘6‘))
def deco(func): print("this is deco") def in_deco(x,y): print(‘this is closure‘) func(x,y) return in_deco @deco #装饰器的语法糖,会将其下面的函数当做函数对象实参func传入deco, 相当于调用了bar = dec(bar) def bar(x,y): print("this is bar") print("%d+%d=%d" % (x,y,x+y)) bar(2,4)
标签:this 部分 元组 函数名 enc 分享 它的 glob else
原文地址:https://www.cnblogs.com/ladawn/p/8918558.html