上一页 1 ··· 36 37 38 39 40 41 42 43 44 ··· 46 下一页
摘要: #decorator意思:1.装饰器 2.语法糖 import time user,passwd='qi','123' def auth(func): def wrappper(*args, **kwargs): username = input('Username:').strip() password = input('password:').str... 阅读全文
posted @ 2018-01-29 23:05 耐烦不急 阅读(115) 评论(0) 推荐(0) 编辑
摘要: import time def timer(func): #timer(test1) func=test1 def deco(*args,**kwargs): start_time=time.time() func(*args,**kwargs) #run test1() stop_time = time.time() ... 阅读全文
posted @ 2018-01-29 12:16 耐烦不急 阅读(152) 评论(0) 推荐(0) 编辑
摘要: #嵌套函数 def foo(): print('in the foo') def bar(): print('in the bar') bar() #bar()#出错,无法在外边调用,bar函数的作用范围只在foo函数之内 foo() 阅读全文
posted @ 2018-01-28 21:07 耐烦不急 阅读(117) 评论(0) 推荐(0) 编辑
摘要: import time def timer(func):#timer(test1) func=test1 def deco(): start_time=time.time() func()#run test1 stop_time=time.time() print('the func time is %s'%(stop_t... 阅读全文
posted @ 2018-01-28 21:07 耐烦不急 阅读(122) 评论(0) 推荐(0) 编辑
摘要: #匿名函数,无函数名 calc=lambda x:x*3 print(calc(3)) sum=lambda x,y,z:x+y+z print(sum(1,2,3)) ''' 高阶函数 a:把一个函数名当做一个实参传递给另一个函数(在不修改被饰函数源代码的情况下为其添加功能) b:返回值包含函数名(不修改函数的调用方式) ''' def bar(): print('... 阅读全文
posted @ 2018-01-28 21:06 耐烦不急 阅读(190) 评论(0) 推荐(0) 编辑
摘要: 装饰器:定义:本质是函数,(装饰其他函数),就是为其它函数添加附加功能原则:1.不能修改被饰的函数的源代码 2.不能修改被饰的函数的调用方式实现装饰器知识储备: 1.函数即“变量” 2.高阶函数 3.嵌套函数高阶函数+嵌套函数=》实现装饰器 import time def timeer(func): def warpper(*args,**kwa... 阅读全文
posted @ 2018-01-26 14:45 耐烦不急 阅读(197) 评论(0) 推荐(0) 编辑
摘要: 变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。 def add(a,b,f): return f(a)+f(b) res=add(-3,-4,abs) print(res) 阅读全文
posted @ 2018-01-25 14:23 耐烦不急 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。(最大调用自己999次) def calc(n): print(n) if int(n/2)>0: return calc(int(n/2)) print('->',n) calc(10) 递归特性:1. 必须有一个 阅读全文
posted @ 2018-01-25 14:22 耐烦不急 阅读(187) 评论(0) 推荐(0) 编辑
摘要: school='Hebut'#school为全局变量 sex='male'#全局变量 names=['Wang Yu','Bai Jingyi','Zhang Yu'] hobby='姑娘' def change_name(name): global hobby#在函数中修改全局变量,记住以后别用这个方式 hobby='study' print('before chang... 阅读全文
posted @ 2018-01-25 14:20 耐烦不急 阅读(310) 评论(0) 推荐(0) 编辑
摘要: logger函数的定义要放在函数调用之前,在test1(1,2)前边,一下两种都可以 def test1(x,y): print(x,y) logger('Test1') def logger(source): print("来自于%s"%source) test1(1,2) def logger(source): print("来自于%s"%source)... 阅读全文
posted @ 2018-01-25 14:19 耐烦不急 阅读(162) 评论(0) 推荐(0) 编辑
上一页 1 ··· 36 37 38 39 40 41 42 43 44 ··· 46 下一页