Python Learning 11th Day Decorator

Source: Internet
Author: User
Tags closure wrapper

1. Homework explanation

# 2, write a function, receive n numbers, and the number of these parameters.
def sum_func (*args):
Total = 0
For i in args:
Total + = I
Return Total
Print (Sum_func (1,2,3,8,23,6))

# 3, read the code, answer: In the code, print out the value of a,b,c, what are the respective? Why?
# a=10
# B=20
# def test5 (A, B):
# Print (A, B)
# C = test5 (b,a)
# Print (c)



# 4, read the code, answer: In the code, print out the value of a,b,c, what are the respective? Why?
# a=10
# B=20
# def test5 (A, B):
# A=3
# B=5
# Print (A, B)
# C = test5 (b,a)
# Print (c)

2. Decorative Device
# Adorner Formation process: The simplest adorner has a parameter with a return value of the Universal parameter
# The role of decorators
# principle: Open Closure principle
# syntax Sugar: @
# Decorator's Fixed mode

#不懂技术


Import time
# Print (Time.time ()) # Get current time
# Time.sleep (#让程序在执行到这个位置的时候停一会儿)


# def Timmer (f): #装饰器函数
# def inner ():
# start = Time.time ()
# ret = f () #被装饰的函数
# end = Time.time ()
# Print (End-start)
# return RET
# return Inner
#
# @timmer #语法糖 @ Adorner function name
# def func (): #被装饰的函数
# Time.sleep (0.01)
# print (' Good boss, good colleagues, good people ')
# return ' Good New Year '
# # func = Timmer (func)
# ret = func () #inner ()
# Print (ret)
# The role of adorners--do not want to modify the function call method but also want to add functionality before and after the original function
# Timmer is an adorner function, but it has some decorative effects on a function

# principle: Open Closure principle
# Open: Open to Extensions
# closed: Closed for modification

# sealed version

# DEF outer ():
# def inner ():
# return ' inner '
# inner ()
#
# outer ()

#装饰带参数函数的装饰器
# def Timmer (f): #装饰器函数
# def inner (*args,**kwargs):
# (+)/(1)
# start = Time.time ()
# ret = f (*args,**kwargs) #f #被装饰的函数
# end = Time.time ()
# Print (End-start)
# return RET
# return Inner
#
# @timmer #语法糖 @ Adorner function name
# def func (A, b): #被装饰的函数
# Time.sleep (0.01)
# print (' Good boss, good colleagues, good people ', A, b)
# return ' Good New Year '
#
# @timmer #语法糖 @ Adorner function name
# def FUNC1 (a): #被装饰的函数
# Time.sleep (0.01)
# print (' Good boss, good colleagues, good people ', a)
# return ' Good New Year '
# # func = Timmer (func)
# ret = Func #inner ()
# ret = func (1,b = 2) #inner ()
# Print (ret)

# Def wrapper (f): #装饰器函数, F is the decorated function
# def inner (*args,**kwargs):
# "Things to do before being decorated function"
# ret = f (*args,**kwargs) #被装饰的函数
# ' What to do after being decorated function '
# return RET
# return Inner
#
# @wrapper #语法糖 @ Adorner function name
# def func (A, b): #被装饰的函数
# Time.sleep (0.01)
# print (' Good boss, good colleagues, good people ', A, b)
# return ' Good New Year '
# Print (func)

# def wrapper ():
# def inner ():
# Pass
# return Inner

# def wrapper (func): #qqxing
# def inner (*args,**kwargs):
# ret = func (*args,**kwargs) #被装饰的函数
# return RET
# return Inner
#
# @wrapper #qqxing = wrapper (qqxing)
# def qqxing ():
# print (123)
#
# ret = qqxing () #inner

#
# Explaining the New Year's Day job extension decorator




Python Learning 11th Day Decorator

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.