'''函数扫盲''' '''函数式完成一个记录日志的小功能''' ''' def log(): import time time_format = '%Y-%m-%d %X' time_curren = time.strftime(time_format) with open('Log','a+') as log: log.write('%s logger \n'%time_curren) def fun01(): print("This is fun 01") log() def fun02(): print("This is fun 02") log() def fun03(): print("This is fun 03") log() fun01() fun02() fun03() ''' ''' def fun01(): print("This is function01") def fun02(): print("This is function02") return 0 def fun03(): print("This is function03") return 1,'A',[1,2,3,4],(1,2,3,4),{"name":"Python"} def fun04(): print("This is function04") return fun02() #x = fun01() #y = fun02() #z = fun03() #m = fun04() #print(x) #print(y) #print(z) #函数的返回值可以为none,数值,也可以为多个值,返回的时候会打包成元组。 ''' '''形参和实参一一对应''' ''' def fun01(x,y,z=1): #定义形参x,y,默认参数z。默认参数已经有默认赋值,如果引用函数给默认参数赋值会覆盖掉默认值。 print('sum is %d' %(x+y)) print('z is %d'%z) return x+y fun01(1,2) #实参 fun01(x=1,y=2) #fun01(y=1,2) #位置变量不能颠倒 #fun01(x=1,2) x=1 y=2 z=3 fun01(x=x,y=z,z=y) #如果给默认参数传递值,会覆盖默认值。 ''' '''不确定数量的形参——参数组''' ''' def fun01(*args): #定义不确定数目的形参 "将传入的参数求和" sum = 0 for arg in [*args]: sum += arg print('sum is %d'%sum) print(type(args)) print(args) fun01(1,2,3,4) fun01(*[1,2,3,4,5]) #以这种方式传入列表,每个元素都是一个参数。 def fun02(**kwargs): #以字典的方式定义形参 for i in kwargs: print(i) print(type(kwargs)) fun02(name='yufo',age='24',job='it') #传入的实参会保存为字典 def fun03(a,*args,**kwargs): #一个函数实例说明确参数关系 print('a is %d'%a) print(args) print(kwargs) fun03(1,2,3,4,5,name='yufo',job='IT') ''' '''全局变量和局部变量''' '''函数内部定义的变量是局部变量,作用于只在函数内部''' ''' name = 'root' #全局变量,默认函数体不能引用 job = 'Network' list=['Apple','Samsung','Huawei','Xiaomi'] tuple=tuple(list) dict = {1:'Apple',2:'Samsung'} def check_name(): global name #函数调用全局变量需要用global,同时可以对全局变量进行修改 print(name) name = 'yufo' print(name) def check_job(): job = 'Cloud' #局部变量,作用于只在函数体内 print(job) def check_list(): print(list) #列表全局可以访问,也可以直接修改。数字、字符串需要Global才可以 list[0] = '苹果' print(list) def check_tuple(): print(tuple) #元组同列表,但是无法修改 def check_dict(): print(dict) #字典也可以全局访问,也可以直接修改 dict[1] = '苹果' print(dict) check_name() print('Now global name is %s'%name) check_job() print('Now global job is %s'%job) #发现全局变量并没有被修改 check_list() print('Now global list is %s'%list[0]) check_tuple() check_dict() print('Now global dict is %s'%dict[1]) ''' '''高阶函数,把函数作为参数传给函数''' def fun01(a,b,f): return f(a)+f(b) print(fun01(1,-2,abs)) #abs是求绝对值的函数,作为参数传给了fun01
''' 装饰器是在不改写函数及调用方式的前提下增加或修改函数的功能。主要用到了函数嵌套和高阶函数。 ''' import time def deco(func): #定义装饰器,定义形参用来传入函数 def timer(*args,**kwargs): #定义嵌套函数,实现记录运行时间的功能,由于参数不固定使用参数组来存放形参 start_time = time.time() #记录开始时间 func(*args,**kwargs) #将参数组传给初始函数 stop_time = time.time() #记录截止时间 print('The func\'s running time is %s.\n'%(stop_time-start_time)) return timer #返回附加功能的值 @deco #语法糖 和test1 = deco(test1)等价 def test1(): #定义初始函数test1 time.sleep(2) print('This is test1') @deco def test2(name): #定义初始函数test2 time.sleep(2) print('Your name is %s'%name) test1() test2('Yufo')
''' 装饰器多层用法 ''' def auth(auth_type): def deco1(func): def deco(): if auth_type == 'local': username = 'yufo' password = 'rOOT' user = input('Input your username:') passwd = input('Input your password:') if user == username and passwd == password: print('Welcome.') func() else: print('Invalid username or password.') elif auth_type == 'ldap': print('Not yet.\n') return deco return deco1 @auth(auth_type = 'local') def index(): print('You are in index.') @auth(auth_type = 'local') def home(): print('You are in home.') @auth(auth_type = 'ldap') def bbs(): print('You are in bbs.') index() home() bbs()