高阶函数:
#Author:Brill 高阶函数 def add(a,b,f): return f(a)+f(b) res = add(3,-6,abs) print(res)
递归:
#Author:Brill """ def calc(n): print(n) return calc(n+1) calc(0) """ def calc(n): print(n) if int(n/2) > 0: return calc(int(n/2)) print("->",n) calc(10)
局部变量:
#Author:Brill ''' school = "Oldboy edu." def change_name(name): global school school = "Linux" print("before change",name,school) name = "Alex li"#这个函数就是这个变量的作用域 age = 23 print("After change",name,school) name = 'alex' change_name(name) print(name) print(school) ''' school = 'Oldboy edu.' names = ["Alex","Jack","Rain"] def change_name(): print(names) names[0]='King' print('Inside func',names) change_name() print(names)
关键字参数:
#Author:Brill ''' #args:接受位置参数 def test(x,*args): print(args) test(1,2,3,4,5) test(*[1,2,3,4,5,5]) #**kwargs:把N个关键字参数,转换成字典的方式 def test2(**kwargs): print(kwargs) print(kwargs['name']) print(kwargs['age']) print(kwargs['sex']) test2(name = 'alex',age = 8,sex = 'F') test2(**{'name':'alex','age':8,'sex':"F"}) ''' def test3(name,**kwargs): print(name) print(kwargs) test3("alex",age =18,sex = 'F') def test4(name,age = 18,**kwargs): print(name) print(age) print(kwargs) logger("test4") def logger(source): print("from %s" % source) test4('alex',sex = 'm',hobby = 'tesla')