Python 3.x--Decorator

Source: Internet
Author: User
Tags wrapper

———————— adorner = higher order function + nested function ——————————

High order function: 1, pass a function name as an argument to another function, 2, the return value contains the function name

Def func1 ():
Print ("This is Func1")

def test (func):
Print (func)
Func ()

Test (FUNC1)

Output Result:

Adorner: Add additional functions to other functions without changing the original function code and calling method

Import time

def bar ():
Time.sleep (2)
Print ("This is Bar")

def test1 (func1):
Print (FUNC1)
Return func1

Bar = test1 (bar)
Bar ()


Does not change the calling method, no new features are added---------

Import time
def deco (func):
Start_time = Time.time ()
Return func
Stop_time = Time.time ()
Print (' Running time is%s: '% (stop_time-start_time))

def test1 ():
Time.sleep (2)
Print ("This is Test1")
Def test2 ():
Time.sleep (2)
Print ("This is Test2")

Test1 = Deco (test1)
Test1 ()
Test2 = Deco (test2)
Test2 ()

Operation Result:

Add new features without changing the calling method---------

Import time
def timer (func):
Def deco ():
Start_time = Time.time ()
Func ()
Stop_time = Time.time ()
Print (' Running time is%s: '% (stop_time-start_time))
Return deco

def test1 ():
Time.sleep (2)
Print ("This is Test1")
Def test2 ():
Time.sleep (3)
Print ("This is Test2")

Test1 = Timer (test1)
Test1 ()
Test2 = Timer (test2)
Test2 ()

Operation Result:

Use the @ symbol-----------

Import time
def timer (func):
Def deco ():
Start_time = Time.time ()
Func ()
Stop_time = Time.time ()
Print (' Running time is%s: '% (stop_time-start_time))
Return deco
@timer
def test1 ():
Time.sleep (2)
Print ("This is Test1")
Def test2 ():
Time.sleep (3)
Print ("This is Test2")

Test1 ()
Test2 = Timer (test2)
Test2 ()

The original function has parameter-----------

Import time
def timer (func):
def deco (*arg,**kwarg):
Start_time = Time.time ()
Func (*arg,**kwarg)
Stop_time = Time.time ()
Print (' Running time is%s: '% (stop_time-start_time))
Return deco
@timer
def test1 ():
Time.sleep (2)
Print ("This is Test1")
@timer #test2 = Timer (test2)
def test2 (name,age):
Time.sleep (1)
Print ("This is test2:", Name,age)

Test1 ()
Test2 (' Lili ', 18)

Operation Result:

The original function has a return value----------

user,passwd = ' Lili ', ' abc '
def auth (func):
def wrapper (*args,**kwargs):
Username = input ("username:"). Strip ()
Password = input ("PASSWD:"). Strip ()

If user = = Username and passwd ==password:
Print ("\033[32;1muser has passed authenticatiom\033[0m")
return func (*args,**kwargs)
Else
Exit ("\033[31;1minvalid username or password\033[0m")
Return wrapper
@auth
Def Page1 ():
Print ("This is Page1")
Return "from Page1"
@auth
Def page2 ():
Print ("This is Page2")
@auth
Def page3 ():
Print ("This is Page3")


Print (Page1 ())

Operation Result:

Adorner with parameter-------------

 user,passwd = ' Lili ', ' abc ' 
DEF Auth (auth_type):
Print ("Auth_arge:", Auth_type)
def outer_wrapper (func ):
def wrapper (*args,**kwargs):
If Auth_type = = ' P1 ':
Username = input ("Usern Ame: "). Strip ()
Password = input (" PASSWD: "). Strip ()

If user = = Username and PASSW D ==password:
Print ("\033[32;1muser has passed authenticatiom\033[0m")
Retur n func (*args,**kwargs)
Else:
Exit ("\033[31;1minvalid username or password\033[0m ")
elif Auth_type = = ' P2 ':
print (' P2 pass ')
return wrapper
Return Oute R_wrapper

@auth (auth_type = "P1")
Def Page1 ():
Print ("This is Page1")
return ' from Page1 '
@auth (Auth_type = "P2")
def page2 ():
Print ("This is Page2")

Print (Page1 ())
Page2 ()

Operation Result:

Python 3.x--Decorator

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.