Python 11th days, dnf 11th sets of sky Sets

Source: Internet
Author: User

Python 11th days, dnf 11th sets of sky Sets

Start learning the second module:

Decorator:

Description:

Decorator principles:

1. the source code of the decorated function cannot be modified.

2. The caller cannot modify the decoration function

 

Decorator:

Higher-order functions + nested functions = decorator

Higher-order functions:

Type I: a function that uses a function as a real parameter. It can be called a high-order function.

        

1 def fun_1 (fun_2): # fun_1 is a high-order function 2 print ('1') 3 fun_2 () 4 5 def fun_2 (): 6 print ('2 ') 7 8 fun_1 (fun_2)

 

Type II: A function whose return value contains the function name, also known as a higher-order function

      

Def fun_3 (fun): # fun_3 is the high-order function print ('3') return fundef fun_4 (): print ('4') # fun_3 (fun_4 )() # Run fun = fun_3 (fun_4) directly for the returned value # assign the returned value to a variable fun () # Call a function.

 

Nested functions:

Define a new function in a function. This function is called a nested function.

    

1 def fun_5 (): # fun_5 is the nested function 2 print ('5') 3 def fun_5_1 (): 4 print ('5. 1') 5 6 fun_5 ()

The simplest Decoration:

1 def fun_2 (fun): # input fun_1 function 2 def function (): 3 4 fun () 5 print ('in the fun_2 ...... ') # Add new content 6 7 return function 8 9 @ fun_2 # decorate fun _! Equivalent to fun_1 = fun_2 (fun_1) 10 def fun_1 (): 11 print ("in the fun_1...") 12 13 14 fun_1 ()

Decorator: Simulated login authentication

1 # simulate website login and access authentication 2 #3 name = 'abc' 4 password = '000000' 5 def certi (model): # decorator 6 def outr (fun ): # add a layer of nested 7 def login (* args, ** kwargs) to the modifier and parameters: # to be compatible with various function parameters, add * args, ** the kwargs parameter is not fixed. 8 if model = 'Password': 9 print ('this is password Authentication') 10 user_name = input ('username :'). strip () 11 paswd = input ('password :'). strip () 12 if user_name = name and paswd = password: 13 print ('authenticated ') 14 return fun (* args, ** kwargs) 15 else: 16 print ('user or Password error, exit ') 17 Exit () 18 elif model = 'lamp ': 19 print ('this is lamp Authentication') 20 return fun (* args, ** kwargs) 21 else: 22 print ('authentication error') 23 return login24 return outr25 26 27 def indxe (): 28 print ('Welcome to homepage ') 29 30 @ certi (model = 'Password ') 31 def user (): 32 print ('Welcome to the user page: ') 33 34 @ certi (model = 'lamp') 35 def bbs (name ): 36 print ('Welcome % s to log on to the Forum !! '% Name) 37 38 indxe () 39 user () 40 bbs (name = 'yjj ')

 

  

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.