裝飾器

函數對象python

 1 #函數是第一類對象:指的是函數能夠當作數據傳遞#函數若是不加()就是引用這個函數,加上()輸出返回值,
 2 #1、能夠被引用 x=1,y=x
 3 # def func(x,y):
 4 #     print(x,y)
 5 #
 6 # f=func
 7 # f(1,2)
 8 #2、可當作函數的參數傳入
 9 # def foo():
10 #     print('from foo')
11 #
12 # def bar(func):
13 #     # print(func)
14 #     func()#這裏指的是拿到函數foo的內存地址了加()就能調用
15 #
16 # bar(foo)
17 
18 #3、能夠當作函數的返回值
19 # def foo():
20 #     print('from foo')
21 #
22 # def bar():
23 #     return foo
24 #
25 # f=bar()#有返回值,要保存就定義個變量
26 # f()#拿到函數foo的內存地址了加()就能調用
27 
28 #4、能夠當作容器類型的元素
29 # def foo():
30 #     print('from foo')
31 #
32 # def bar():
33 #     return foo
34 #
35 # l=[foo,bar]
36 # print(l)
37 # l[0]()
38 
39 
40 # def get():
41 #     print('get')
42 #
43 # def put():
44 #     print('put')
45 #
46 # def ls():
47 #     print('ls')
48 #
49 # cmd=input('>>: ').strip()
50 # if cmd == 'get':
51 #     get()
52 # elif cmd == 'put':
53 #     put()
54 # elif cmd == 'ls':
55 #     ls()
56 
57 
58 
59 
60 def get():
61     print('get')
62 
63 def put():
64     print('put')
65 
66 def ls():
67     print('ls')
68 
69 def auth():
70     print('auth')
71 
72 func_dic={
73     'get':get,
74     'put':put,
75     'ls':ls,
76     'auth':auth
77 }
78 
79 # func_dic['put']()
80 cmd = input('>>: ').strip()
81 if cmd in func_dic:
82     func_dic[cmd]()

 函數嵌套mysql

 1 #1、函數的嵌套調用
 2 # def my_max(x,y):
 3 #     if x >= y:
 4 #         return x
 5 #     else:
 6 #         return y
 7 #
 8 # def my_max4(a,b,c,d):
 9 #     res1=my_max(a,b)
10 #     res2=my_max(res1,c)
11 #     res3=my_max(res2,d)
12 #     return res3
13 
14 
15 #2、函數的嵌套定義
16 def f1():
17     def f2():
18         print('from f2')
19         def f3():
20             print('from f3')
21         f3()
22     # print(f2)
23     f2()
24 
25 
26 f1()
27 # f2#在函數內部定義的函數只能在內部用(在當前級別定義的函數只能在當前級別使用)

名稱空間sql

  1 #名稱空間指的是:存放名字與值綁定關係的地方,
  2 
  3 #內置名稱空間(python解釋器啓動就有):python解釋器內置的名字,max,len,print
  4 #全局名稱空間(執行python文件時生效):文件級別定義的名字(定頭寫),不是內置的也不是函數內部定義的
  5 # 1.x=1
  6 # 2.def func():pass
  7 # 3.import time
  8 # 4.if x == 1:
  9 #     y=2
 10 
 11 #局部名稱空間(函數調用時生效,調用結束失效):函數內部定義的名字,
 12 # func()
 13 
 14 #加載順序:內置---》全局----》局部名稱空間
 15 #訪問名字的順序:局部名稱空間===》全局----》內置
 16 # x=1
 17 # print(x)
 18 
 19 # print(max)
 20 
 21 # max=2
 22 # def func():
 23 #     # max=1#把它註釋掉就是不從局部找,就要從全局找,全局找到max=2,就會把值給func()
 24 #     print(max)
 25 #
 26 # func()
 27 
 28 
 29 # x='gobal'
 30 # def f1():
 31 #     # x=1
 32 #     def f2():
 33 #        # x=2
 34 #        def f3():
 35 #            # x=3
 36 #            print(x)
 37 #        f3()
 38 #     f2()
 39 #
 40 # f1()
 41 
 42 
 43 
 44 #全局做用域(全局範圍):內置名稱空間與全局名稱空間的名字,全局存活,全局有效,globals()#查看全局做用於的名
 45 #局部做用域(局部範圍):局部名稱空間的名字,臨時存活,局部有效,locals()#查看局部做用於的名
 46 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=111111111111111111111
 47 # print(globals())
 48 # print(dir(globals()['__builtins__']))#先不用考慮
 49 
 50 # print(locals() is globals())#結果是True
 51 
 52 # def func():
 53 #     yyyyyyyyyyyyyyyyyyyyyyyy=22222222
 54 #     print(globals())
 55 #     print(locals())
 56 #
 57 # func()
 58 
 59 
 60 # x=100
 61 # def func():
 62 #     global x#加上這個指的這個x是全局的名字,print(x)爲100,不加這個指的是局部的名字,因此print(x)爲100
 63 #     x=1
 64 #
 65 # func()
 66 # print(x)
 67 
 68 
 69 
 70 # x='global'
 71 # def f1():
 72 #     # x=1
 73 #     def f2():
 74 #         nonlocal x#改當前層函數的外面一層
 75 #         x=0
 76 #     f2()
 77 #     print('===f1 innter--->',x)
 78 #
 79 # f1()
 80 # print(x)
 81 
 82 
 83 
 84 
 85 
 86 #強調兩點:
 87 #1、打破函數層級限制來調用函數
 88 # def outter():
 89 #     def inner():
 90 #         print('inner')
 91 #     return inner#上面函數中定義完了的函數能夠作返回值來調用
 92 #
 93 # f=outter()
 94 # # print(f)
 95 #
 96 # def bar():
 97 #     f()
 98 # bar()
 99 
100 
101 #2、函數的做用域關係是在函數定義階段就已經固定了,與調用位置無關
102 x=1
103 def outter():
104     # x=2
105     def inner():
106         print('inner',x)
107     return inner
108 
109 f=outter()
110 # print(f)
111 # x=1111111111111111111111111111111111111111111111111111111111111111111111111111111111#把值放到這個地方,指的是在下面f()再調用的時候全局名稱空間的值已經變成這個值了
112 def bar():
113     x=3
114     f()
115 # x=1111111111111111111111111111111111111111111111111111111111111111111111111111111111#這依然是在全局的
116 bar()
117 x=1111111111111111111111111111111111111111111111111111111111111111111111111111111111#上面已經執行完因此值不會變
118 
119 
120 def outter():
121     def inner():
122         print('inner')
123 
124     return inner
125 f=outter()
126 print(f)
127 
128 def bar():
129     f()
130 bar()#bar調用的是f這個名稱空間的值

閉包函數閉包

 1 #閉包函數:
 2 #1 定義在函數內部的函數
 3 #2 該函數的函數體代碼包含對外部做用域(而不是全局做用域)名字的引用
 4 #3 一般將閉包函數用return返回,而後能夠在任意使用
 5 #def outer():
 6 #    x=1
 7 #    def inner():#這個函數就叫閉包函數
 8 #        print(x)#這個函數體要引用x這個名字,而這個名字在上一級的函數體中就叫閉包函數
 9 #
10 #
11 # z=1
12 # def outer():
13 #     x=1
14 #     y=2
15 #     def inner():
16 #         print(x,y)
17 #         # print(z)
18 #     return inner#定義在函數內部的函數受層級的限制,只能當前位置使用,要想打破層級限制,就要return
19 #
20 # f=outer()
21 # print(f.__closure__[0].cell_contents)#顯示閉包函數的值
22 # print(f.__closure__[1].cell_contents)
23 # print(f.__closure__)
24 
25 
26 # def bar():
27 #     x=111121
28 #     y=2222
29 #     f()#f能夠在這調用
30 #
31 # bar()
32 
33 
34 
35 兩種給函數傳參
36     (1)經過參數傳給他
37 # def foo(x,y):
38 #     print(x+y)
39 #
40 # foo(1,2)
41     (2)把參數的值報給他(閉包函數)
42 # def outter():
43 #     x=1
44 #     y=2
45 #     def foo():
46 #         print(x+y)
47 #     return foo
48 #
49 #
50 # f=outter()
51 #
52 # f()
53 
54 
55 
56 #爬頁面:閉包函數爲咱們提供了一種新的爲函數傳參的方式
57 import requests #pip3 install requests
58 
59 # def get(url):
60 #     response=requests.get(url)
61 #     if response.status_code == 200:
62 #         print(len(response.text))
63 #
64 # get('https://www.baidu.com')
65 # get('https://www.baidu.com')
66 # get('https://www.baidu.com')
67 
68 def outter(url):
69     # url = 'https://www.baidu.com'
70     def get():
71         response=requests.get(url)
72         if response.status_code == 200:
73             print(len(response.text))
74     return get
75 
76 baidu=outter('https://www.baidu.com')
77 python=outter('https://www.python.org')
78 # baidu()
79 # baidu()
80 # baidu()

裝飾器函數

 1 #1、開放封閉原則:對擴展開放,對修改是封閉
 2 
 3 #2、裝飾器:裝飾它人的,器指的是任意可調用對象,如今的場景裝飾器-》函數,被裝飾的對象也是-》函數
 4 #原則:1、不修改被裝飾對象的源代碼 2、不修改被裝飾對象的調用方式
 5 #裝飾器的目的:在遵循1,2的前提下爲被裝飾對象添加上新功能
 6 
 7 #錯誤的示範
 8 # import time
 9 #
10 # def index():
11 #     time.sleep(3)
12 #     print('welecome to index')
13 #
14 # def timmer(func):#函數體代碼須要參數至關於func=某個值
15 #     start=time.time()
16 #     func()
17 #     stop=time.time()
18 #     print('run time is %s' %(stop-start))
19 #
20 # timmer(index)
21 
22 
23 
24 import time
25 def index():
26     time.sleep(3)
27     print('welecome to index')
28 
29 def timmer(func):
30     # func=index #最原始的index#這個值是要個inner下面的
31     def inner():
32         start=time.time()
33         func() #最原始的index
34         stop=time.time()
35         print('run time is %s' %(stop-start))
36     return inner
37 
38 index=timmer(index) #index=inner
39 # print(f)
40 index() #inner()

加上段點就是要走import time,def index,def timmer,index=timmer(index),從這幾個走的,當走到index=timmer(index)的時候
就會調用timmer這個函數,就會把最初的index傳給func了,就會到def inner,這就到index=timmer(index),括號中的index就是inner
的值
 1 #裝飾器語法:在被裝飾對象正上方單獨一行寫上,@裝飾器名
 2 
 3 # #改進一:
 4 # import time
 5 # def timmer(func):
 6 #     def inner():
 7 #         start=time.time()
 8 #         res=func()
 9 #         stop=time.time()
10 #         print('run time is %s' %(stop-start))
11 #         return res
12 #     return inner
13 #
14 # @timmer #index=timmer(index)#這個@符號指的是把@timmer正下方的函數名看成參數傳給timmer這個函數(timmer(index)),而且把這個結果從新命名給index
15 # def index():
16 #     time.sleep(1)
17 #     print('welecome to index')
18 #     return 1111#若是有返回值,
19 #
20 # res=index() #res=inner()
21 # print(res)
22 
23 
24 #改進二:*args **kwargs
25 import time
26 def timmer(func):
27     def inner(*args,**kwargs):
28         start=time.time()
29         res=func(*args,**kwargs)
30         stop=time.time()
31         print('run time is %s' %(stop-start))
32         return res
33     return inner
34 
35 # @timmer #index=timmer(index)
36 # def index(name):
37 #     time.sleep(1)
38 #     print('welecome %s to index' %name)
39 #     return 1111
40 # res=index('egon') #res=inner('egon')
41 # print(res)
42 
43 
44 
45 import time
46 def timmer(func):
47     def inner(*args,**kwargs):
48         start=time.time()
49         res=func(*args,**kwargs)
50         stop=time.time()
51         print('run time is %s' %(stop-start))
52         return res
53     return inner
54 @timmer #home=timmer(home)
55 def home(name):
56     print('welcome %s to home page' %name)
57 
58 home('egon') #inner('egon')

 

 有參裝飾器ui

 1 import time
 2 def auth(engine='file'):
 3     def outter(func):
 4         def inner(*args,**kwargs):   #這裏接受任意
 5             if engine == 'file':
 6                 name=input('name>>>').strip()
 7                 passwd=input('passwd>>>').strip()
 8                 if name == 'lqx' and passwd == '123':
 9                     print('login seccussfull')
10                     res=func(*args,**kwargs)   #那麼這裏也應該有參數
11                     return res
12                 else:
13                     print('login err')
14             elif engine == 'mysql':
15                 print('login')
16             else:
17                 print('login err')
18         return inner
19     return outter
20 # engine='file'
21 @auth(engine='file')  #outter #index=auth(index)
22 #@outter
23 def index(name,*args,**kwargs):  #若是這裏有參數
24     time.sleep(0.5)
25     print('welecome to index %s'%name)
26     return 11112312312312312
27 a=index('lqx',18,231312)   #若是這裏有參數,那麼上面的inner也應該有接受參數的形參
28 print(a)

並行多個裝飾器url

這裏的意思的用倆個裝飾器,第一個裝飾器修飾第二個裝飾器,第二個裝飾器修飾下面的原函數。所以,這裏的第一個裝飾器必定是一個能夠徹底修飾任意原函數的一個裝飾器

  

 1 import time
 2 
 3 def timmer(func):
 4     def inner(*args,**kwargs):
 5         start=time.time()
 6         res=func(*args,**kwargs)
 7         stop=time.time()
 8         print(stop - start)
 9         return res
10     return inner
11 def auth2(engine='file'):
12     def auth(func):
13         def inner(*reags,**kwargs):
14             if engine == 'file':
15                 name=input('name>>>').strip()
16                 passwd=input('passwd>>>').strip()
17                 if name == 'lqx' and passwd == '123':
18                     print('login successfull')
19                     res=func(*reags,**kwargs)
20                     return res
21                 else:
22                     print('login err')
23             elif engine == 'mysql':
24                 print('mysql auth')
25             else:
26                 print('login err')
27         return inner
28     return auth
29 @timmer
30 @auth2(engine='file')
31 def index(name,*args):
32     time.sleep(1)
33     print('welecome %s to index'%name)
34     return 12341234123123123123123131231232132131231231
35 
36 a=index('lqx',123,'age','sex')
37 print(a)

warps註釋信息假裝spa

1 使用模塊wraps,實際上這個wraps也是一個裝飾器, 
2 只要把@wraps,放到原函數的傳值的上面就能夠實現 
3 而後在原函數的後面執行print(help(原函數)),就能夠看到原函數的註釋信息
 1 from functools import wraps    #註釋信息假裝
 2 import time
 3 def timmer(func):
 4     @wraps(func)   #註釋信息模塊
 5     def inner(*args,**kwargs):
 6         start=time.time()
 7         res=func(*args,**kwargs)
 8         stop=time.time()
 9         print(stop - start)
10         return res
11     # inner.__doc__ = func.__doc__  # 把index的註釋信息賦值給inner
12     # inner.__name__=func.__name__   #把index的註釋新的的名字也賦值給inner
13     return inner
14 @timmer
15 def index(name):
16     '''index 函數。。。。。'''
17     time.sleep(1)
18     print('welecome %s to index'%name)
19     return 12341234123123123123123131231232132131231231
20 print(help(index))
相關文章
相關標籤/搜索