Python Full Stack development * 11 Knowledge Point Summary * 1806011

Source: Internet
Author: User
Tags closure wrapper

I. Operation of function name, class object
function name is a variable, but it is a special variable, with parentheses can be a function of the variable
1. Memory address of function name
def func (FN):
Print (FN)
Print (func) #结果: The memory address of the printing function <function func at 0x0000028aa22f2ea0>
2. Function names can be assigned to other variables
Def func1 ():
Print ("Dragon Boat Festival")
B=func1 #将函数名func1赋值给b,
B () #执行结果和 func1 the same as the results of "Dragon Boat Festival"
3. Function names can be used as elements of a container class
Def FUNC2 ():
Print ("Beibei")
Def func3 ():
Print ("Beijing-Beijing")
Def FUNC4 ():
Print ("Huan Huan")
LST=[FUNC2,FUNC3,FUNC4] #如果加括号打印 E, the result is three none. Because there is no return value.
For E in LST:
E () #结果是 Bebekingin Huan Huan;
4. Function names can be used as arguments to functions
Def FUNC5 ():
Print ("Dragon Boat Festival to holiday, hahaha")
def func6 (FN): # fn= Func5
Print ("Next week is the Dragon Boat Festival") #第一步
fn () #第二步 equivalent to executing FUNC5 () The result is: Dragon Boat Festival to holiday, hahaha
Print ("really") #第三步
Func6 (FUNC5) #func5 as a parameter,
5. Function names can be used as return values for functions
Def func7 ():
Print ("function one")
Def FUNC8 ():
Print ("function two")
Print ("function one")
Return Func8
Ret=func7 () #func7 () The result of estoppel is FUNC8 and assigned to RET
RET () #ret () is equivalent to the FUNC8 () result is "function two"
?. The closure is the inner layer function, the external layer function (non-global) variable of the citation?
The role of closures allows variables to reside in memory. The procedure for the post ?
def func ():
Name= "Mary"
Def func1 ():
Print (name) #闭包 returns none if it is # name = "Alex".
Func1 ()
Print (func1.__closure__) #_ _closure_ _ to detect if a function is a closure, the return cell is a closure, (<cell at 0x000002af8f9475b8:str object at 0x000002af8 F9d89d0>,)
# return none is not a closure.
Func ()
#多层嵌套:
Def func1 ():
Def FUNC2 ():
Def func3 ():
Print ("hehe")
Return func3
Return FUNC2
Func1 () () ()
three. Decorator First Knowledge
The open and closed principle, the opening and closing principle, the extension of your code to functionality is open, and your program is closed for modifying the source code. Such software design ideas can be
better maintenance and development.
1. Define a function
Def creat_people ():
Print ("Tuan clay, pinch clay figurines")
Creat_people ()
2. Adding features
Def creat_people ():
Print ("Watering") #增加功能 (but changed the code, not in accordance with the open closure principle)
Print ("Tuan clay, pinch clay figurines")
Creat_people ()
"""Adorner Execution Flow
1. Visit Warter (create_people) first.
2. Pass your sign function to the parameter fn of the warter. So after? If you hold the FN, it means you have your own. Standard function Create_people
3. Warter () A sentence. Returns the inner function. This time. The program thinks that the Warter () function is finished. So the create_people of the former?
be re-overwritten into the inner function
4 the Create_people function. Actually, the inner function is the one that is being accessed in the inner.
The original Create_people function that was passed in
"""
3. The prototype of the adorner simplifies code syntax with syntactic sugar syntax (@ Adorner)
def water (FN):
def inner ():
Print ("watering")
FN ()
Print ("Blow fairy Gas")
return inner
@water
Def creat_people ():
Print ("Tuan clay, pinch clay figurines")
Creat_people ()
4. Complete model code for adorners
def wrapper (func):
def inner (*args,**kwargs): # * Aggregation
Print ("Quick Vacation")
#目标函数之前的内容
Ret=func (*args,**kwargs) # * beaten
Print ("Good fun")
#目标函数后面的内容
return ret
return inner
@wrapper #target_func =wrapper (func)
Def target_func ():
Print ("I am the target function, you all give way")
Target_func ()

Python Full Stack development * 11 Knowledge Point Summary * 1806011

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.