Python full stack development from primer to discard adorner function

Source: Internet
Author: User
Tags wrapper

1. Function name can be used as parameter of function

1 Import Time2 defTimmer (func):3     #function names can be used as arguments to functions4     definner ():5Start =time.time ()6 func ()7End =time.time ()8         Print(End-start)9     returnInnerTen  One defhahaha (): ATime.sleep (0.1) -     Print('AAAA') -  the hahaha () -  - Output Results -Aaaa

2, if we can not modify the function of the call mode, and can not modify the original code, how to do it

1 Import Time2 defTimmer (func):3     #function names can be used as arguments to functions4     definner ():5Start =time.time ()6 func ()7End =time.time ()8         Print(End-start)9     returnInnerTen  One defhahaha (): ATime.sleep (0.1) -     Print('AAAA') -  thehahaha = Timmer (hahaha)#The address of the Timmer function is given to hahaha and then the call is made - hahaha () -  - AAAA +0.10033607482910156

# hahaha = Timmer (hahaha) #timmer函数的地址给了hahaha
# hahaha () #实际上执行的是timmer

3. Function Transfer parameter

1 defTimmer (func):#---> Hahaha2     defInner (*args,**kwargs):#dynamic parameters received from KKK and compressed into tuples3         #args = = (Kwargs) = = {}4         #*args = = **kwargs = = a =1,b = 25Func (*args,**kwargs)#---"KKK #func把值传给kkk并还原, is actually the compression value of the dynamic parameters received before decompression, return to the KKK function6     returnInner7 8 9 defKKK (A, b):Ten     Print(a) One  AKKK = Timmer (KKK)#the Func in the Timmer function is the KKK function and returns the inner function to KKK -KKK (ON)#the KKK function calls and passes the parameter to the inner (dynamic parameter *args), -  the1

Parameters
#实参: Arguments passed in when a function is called
#形参
#位置参数: value must be passed

1 def aaa (A, B): 2 Print (A, b) 3 aaa (+)


#默认参数: Can not pass

1 def BBB (x=10):      # default parameters are not uploaded parameters, with the default parameters, upload the words will use the upload parameters.  2print(x)3#x =4#x = 20 

#动态参数

1 def CCC (*args):#1,2,3,4,5     #*args  2print(args)  34 CCC (1,2,3,4,5)# pass parameters by location

Extract the parameters

1 def CCC (*args):#1,2,3,4,52     print(args)34  #CCC (1,2,3,4,5) #按位置传参数 56 t = (1,2,3,4,5)7 # ((1, 2, 3, 4, 5),) 8 CCC (*T)  #(1, 2, 3, 4, 5)   #解压

Pass Key by keyword

1 def ddd (**kwargs):   #Kwargs receives the parameters and then stores the 2print in a dictionary     (Kwargs )34'a'b')#  pass parameters by keyword

Scatter assignment

1 defCCC (*args):2     Print('CCC:', args)#(1,2,3,4,5)3     defInner (a,b,c,d,e):4         Print('Inner', A,b,c,d,e)5Inner (*args)#* (1,2,3,4,5) Scattered6 7 defInner (a,b,c,d,e):#receive the extracted parameters and assign them to each value8     Print('Inner', A,b,c,d,e)9CCC (1,2,3,4,5)TenInner (1,2,3,4,5)

4. Grammar sugar

1 defTimmer (func):#---> JJJ2     defInner (*args,**Kwargs):3ret = func (*args,**kwargs)#--->ret = jjj ()4         Print(ret)5         returnret6     returnInner7 8@timmer#JJJ = Timmer (JJJ) syntax sugar9 defjjj ():Ten     return123 One  A  - jjj () #ret = JJJ () -  the  - Output Results -123

5. Adorner function

#装饰器的本质: Closure function
#功能: In the case of not changing the way the original function is called, the extension function is added before and after this function
def Timmer (func):
def inner (*args,**kwargs):
"' Add extension code before function call '
ret = func (*args,**kwargs)
"' Add extension code after function call '
return ret
return inner
Open and closed principle of #设计模式 principle
#对扩展是开放的
#对修改是封闭的

6, adorner decoration two functions

1 defWrapper (func):#Decoration2     defInner (*args,**Kwargs):3 4ret = func (*args,**Kwargs)5 6         returnret7     returnInner8 9@wrapper#AAA = wrapper (AAA)Ten defaaa (): One     Print('ASGHKSDLHF') A  -@wrapper#BBB = wrapper (BBB) - defBBB (): the     Print('ASGHKSDLHF') -  -  - aaa () + BBB () -  +  A #Output Result: at #ASGHKSDLHF - #ASGHKSDLHF

7.

Python full stack development from primer to discard adorner function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.