Python's Way function advanced

Source: Internet
Author: User
Tags function definition

Name space

aka Name space, as the name implies is the place to store names, what name? For example, if the variable x=1,1 is stored in memory, where is the name x stored? Namespaces are where the name X and 1 bindings are stored

A total of 3 namespaces, respectively, are as follows

    • Locals: is a namespace within a function, including local variables and formal parameters
    • GLOBALS: global variable, the namespace of the module in which the function is defined
    • Builtins: Namespaces for built-in modules

Different scopes of variables are determined by the namespace in which the variable resides.

Scope is range

    • Global scope: Global survival, globally valid
    • Local range: Temporary survival, locally valid

Viewing the Scope method globals (), locals ()

Scope Lookup Order
level = ‘L0‘n = 22def func():    level = ‘L1‘    n = 33    print(locals())    def outer():        n = 44        level = ‘L2‘        print(locals(),n)        def inner():            level = ‘L3‘            print(locals(),n) #此外打印的n是多少?        inner()    outer()func()

Question: What is the value of the printed n in inner ()?

LEGB representative name Lookup order: Locals, enclosing function, globals, __builtins__

    • Locals are namespaces within functions, including local variables and formal parameters
    • enclosing namespaces for outer nested functions
    • Globals global variables, the namespace of the module in which the function is defined
    • Builtins the namespace of the built-in module
Closed Package

About closures, function definitions and function expressions are in the function body of another function (nested functions). Furthermore, these intrinsics can access all local variables, parameters declared in the external function in which they are located. When one of these intrinsics is called outside the outer function that contains them, a closure is formed. That is, the intrinsic function is executed after the external function returns. When this inner function executes, it still has to access local variables, arguments, and other intrinsic functions of its external function. The values of these local variables, parameters, and function declarations (initially) are the values that are returned by the external function, but are also affected by intrinsic functions.

def outer():    name = ‘alex‘    def inner():        print("在inner里打印外层函数的变量",name)    return innerf = outer() f()

The meaning of closures: The returned function object, not just a function object, also wraps a layer of scope outside the function, which allows the function to take precedence over the scope of its outer envelope wherever it is called

Decorative Device

You are a back-end developer of a video site, and your site has several sections.

def home():    print("---首页----")def america():    print("----欧美专区----")def japan():    print("----日韩专区----")def henan():    print("----河南专区----")

Video just launched early, in order to attract users, you take a free policy, all videos free to watch, quickly attracted a large number of users, free for a period of time, the huge bandwidth cost of the company can not bear, so ready to the more popular several sections, including "Europe and the United States" and "Henan" zone, You get this demand, think about, want to charge first to let its user authentication, certification passed, and then determine whether the user is a VIP paid members can, is the VIP let's see, not the VIP will not let look on the line. You think this demand is very simple, because to the multi-section certification, it should be the authentication function extracted to write a separate module, and then each section of the call can be, and you gently realized the following functions.

#_*_coding:utf-8_*_user_status = False #用户登录了就把这个改成Truedef login():    _username = "alex" #假装这是DB里存的用户信息    _password = "abc!23" #假装这是DB里存的用户信息    global user_status    if user_status == False:        username = input("user:")        password = input("pasword:")        if username == _username and password == _password:            print("welcome login....")            user_status = True        else:            print("wrong username or password!")    else:        print("用户已登录,验证通过...")def home():    print("---首页----")def america():    login() #执行前加上验证    print("----欧美专区----")def japan():    print("----日韩专区----")def henan():    login() #执行前加上验证    print("----河南专区----")home()america()henan()

At this time you are confident to submit this code to your TEAM leader audit, no Chengxiang, no more than 5 minutes, the code was called back, TEAM leader give you feedback is, I now have a lot of modules need to add the authentication module, your code, although the implementation of the function, However, it is necessary to change the code of each module that needs to be authenticated, which directly violates the principle of "open-closed" principle in software development, and simply, it stipulates that the function code that has been implemented is not allowed to be modified, but can be extended, namely:

    • Closed: Implemented function code block should not be modified
    • Open: Open for expansion of existing functions

This principle you are still the first to hear, I rub, once again feel the gap between this wild programmer and the regular army, but anyway, the boss asked this how to achieve it? How to add the authentication function without changing the original function code? You can not think for a moment, had to take this problem home to continue to suppress, daughter-in-law is not at home, to the next door to the old King's house, you just fell quiet, accidentally thought of the solution, do not change the source code yes. Your teacher from Shahe angle King, remember he taught you, high-order function, is to pass a function as a parameter to another function, then the king said, one day, you will use it, did not expect this knowledge point suddenly jumped out from the brain, I only need to write a authentication method, each call needs to verify the function, the direct The function name of this function as a parameter to my verification module is not OK, haha, witty as I, as you pa pa rewrite the previous code.

  #_ *_coding:utf-8_*_user_status = False #用户登录了就把这个改成Truedef Login (func): #把要执行的模块从这里传进来 _        Username = "Alex" #假装这是DB里存的用户信息 _password = "abc!23" #假装这是DB里存的用户信息 global user_status if user_status = = False: Username = input ("User:") password = input ("pasword:") if username = = _username and Password = = _pass  Word:print ("Welcome login ....") User_status = True else:print ("wrong username or    password! ") if user_status = = True:func () # Look here, as long as the validation passed, call the corresponding function Def home (): Print ("---home----") def America (): #login () #执行前加上验证 print ("----Europe----") def Japan (): Print ("----Japan-Korea Zone----") def Henan (): #login () #执行前加上验证 print ("----Henan zone ----") Home () Login (America) #需要验证就调用 login to pass the function to be verified as a parameter to login# home () # America () Login (Henan)  

You are very happy, finally realized the boss's request, do not change the original function code under the premise, to the function of the verification, at this time, the daughter-in-law back, followed by the old king, you two family relations very good, Lao Wang often come to visit, the old King is also yards nong, you share with him you write code, excitement, etc. Lao Wang looked after, and did not boast you, picked up your son, smiled and said, you this code or change it, or you will be expelled, what? Will be expelled, clearly realized the function ah, the old Wang said, yes, you function is realized, but you made a big bogey, what big bogey? You changed the way you call, think, now do not have every need to authenticate the module, you must call your login () method, and the name of your function to pass to you, someone else is not so called before, imagine, if there are 100 modules need authentication, then these 100 modules have to change the calling mode, So many modules must be more than a person to write, so that everyone to modify the calling method to add authentication, you will be scolded dead .... You think Lao Wang is right, but the question is, how to do not change the original function code, do not change the original call method, but also to add authentication? You puzzled for a while, or can not think of, Lao Wang in tease your son play, you say, Lao Wang Ah, quickly give me some ideas, really can't think out, old Wang back to you ask, Lao Wang: Learn anonymous function not? You: You have studied, it's lambda. Old Wang: What is the difference between lambda and normal function? You: The most direct difference is that the normal function definition needs to write the name, but Lambda does not need the old king: Yes, after the lambda is fixed, in order to make multiple calls, can also give it a name? You: Yes, can write plus = lambda x:x+1 like this, later call Plus can be, but this does not lose the meaning of lambda, clearly called anonymous function Ah, you have the name of what use? Lao Wang: I do not want to discuss its meaning with you, I want to let you understand the fact that the Lao Wang took up your son's artboard, wrote the following code:

def plus(n):    return n+1plus2 = lambda x:x+1

Lao Wang: Do these two kinds of writing represent the same meaning? You: Yes Lao Wang: I gave Lambda x:x+1 a name called Plus2, is not equivalent to Def plus2 (x)? You: I rub, you don't say, really, but Lao Wang, what do you want to explain? Lao Wang: Nothing, just want to tell you that assigning a variable name to a function is like Def Func_name is the same effect, such as the plus (n) function below, you can use the plus name when you call, you can also start another name, such as

calc = pluscalc(n)

Do you understand what I'm trying to convey? You:........... This...... Well..... Not very .... Got it.. Lao Wang: .... This..... Oh...... All right.... , then I'll give you a little bit, you wrote the following code called authentication

home()login(america) #需要验证就调用 login,把需要验证的功能 当做一个参数传给login# home()# america()login(henan)

The way you change the call is because the user needs to execute login (Henan), similar to each call. In fact, a little change can be

home()america = login(america)henan = login(henan)

So you, others call Henan, in fact, the equivalent of calling login (Henan), through login in the authentication, will automatically invoke Henan function. You: I rub, but really alas ... , Lao Wang, or you NB ... But, wait, I wrote it so well, when the user calls, it should look like this.

home()america = login(america) #你在这里相当于把america这个函数替换了henan = login(henan)#那用户调用时依然写america()

However, the problem is that the user is not called, your America = Login (America) will be the first to execute their own America ah .... , you should wait for my user to call when the execution is right, do not believe I try to show you ... Lao Wang: Haha, you are right, this will cause this problem? But do you think there's a way out? You: I rub, you point to the train of thought Ah, eldest brother ... How do I know where to go next ... Lao Wang: Forget it, I guess you can't think of it ... Have you learned about nested functions? You: Yes, and then what? Lao Wang: To achieve the beginning you write America = Login (America) does not trigger the execution of your function, just need to define a layer of function in this login, the first call America = Login (America) only call to the outer login, Although this login will execute, but will not trigger authentication, because all the authentication code is encapsulated in the login inside the new defined function, login only return the function name of the inner function, so the next time you execute America (), you will call the inner function ... You:...... What the? What do you mean, I'm forced to ... Lao Wang: Let me show you the code.

def login(func): #把要执行的模块从这里传进来    def inner():#再定义一层函数        _username = "alex" #假装这是DB里存的用户信息        _password = "abc!23" #假装这是DB里存的用户信息        global user_status        if user_status == False:            username = input("user:")            password = input("pasword:")            if username == _username and password == _password:                print("welcome login....")                user_status = True            else:                print("wrong username or password!")        if user_status == True:            func() # 看这里看这里,只要验证通过了,就调用相应功能    return inner #用户调用login时,只会返回inner的内存地址,下次再调用时加上()才会执行inner函数

At this time you carefully the old Wang write code, feel old Wang is really not ordinary people ah, even this kind of strange tricks can think out ... , in the heart silently thanks to you a Daniel neighbor. You: Lao Wang Ah, you this posture very NB Ah, you original? At this time your daughter-in-law Cindy managed laugh aloud, you do not know she smiled a ball ... Lao Wang: Oh, this is not my original AH of course, this is the development of a common play, called the Grammar of Sugar, the official name of the "decorator", in fact, the above wording, but also can be more simple to remove the following code

america = login(america) #你在这里相当于把america这个函数替换了

Just add the following code to the function you want to decorate.

@logindef america():    #login() #执行前加上验证    print("----欧美专区----")def japan():    print("----日韩专区----")@logindef henan():    #login() #执行前加上验证    print("----河南专区----")

The effect is the same. You are happy to play the old Wang teach your new posture, playing with personal overdraft cheap to your "Henan zone" section added a parameter, and then, the result is wrong ...

You: Lao Wang, Lao Wang, how to pass a parameter on it? Lao Wang: That is inevitable, when you call Henan, is actually the equivalent of the call login, your henan first call Henan = login (Henan), Login returns inner memory address, the 2nd time the user calls Henan ("3p"), Actually equivalent to call the time of inner, but your inner definition is not set parameters, but you gave him a parameter, so the natural error is you: But my section needs to pass parameters Ah, you do not let me preach not ah ... Lao Wang: Did not say not to let you preach, a little change can be.

Lao Wang: You can try it again. You: Sure enough, the great God is the great God ... However, what if there are multiple parameters? Lao Wang: .... Dude, you don't have to let me teach you anything, you know? Non-stationary parameters haven't you learned it? *args,**kwargs ... You: Oh ... Can you do that? nb, I'll try again. You are in this new play can not extricate oneself, unexpectedly did not notice the old Wang has left, your daughter-in-law told you that in order not to disturb you overtime, tonight take the child to live with her sister, you think daughter-in-law really thoughtful, finally, you finally took care of all the requirements, fully follow the open-closed principle, the final code

#_ *_coding:utf-8_*_user_status = False #用户登录了就把这个改成Truedef Login (func): #把要执行的模块从这里传进来 def Inner (*args,**kwargs): #        Define a layer function _username = "Alex" #假装这是DB里存的用户信息 _password = "abc!23" #假装这是DB里存的用户信息 global User_status if User_status = = False:username = Input ("User:") password = input ("pasword:") if use            Rname = = _username and Password = = _password:print ("Welcome login ....") User_status = True        Else:print ("Wrong username or password!") if user_status = = True:func (*args,**kwargs) # Look here, as long as the validation passed, call the corresponding function return inner #用户调用login时, will only return the memory of inner Add () to the next inner function Def home (): Print ("---home----") @logindef America (): #login () #执行前加上验证 print ("----Europe and the United States----")    DEF Japan (): Print ("----Japan-Korea Zone----") # @logindef Henan (Style): ':p Aram style: What kind of love to see, it comes in: return: " #login () #执行前加上验证 print ("----Henan zone----") Home () # America = Login (America) #你在这里相当于把america这个函数替换了henan = Login (Henan) # #那用户调用时依然写america () Henan ("3p") 

At this time, you have been tired of the dead, washing on the fast sleep, midnight, toilet, faint heard the next door of the old Wang home has a faint woman's voice came, you know a smile, Lao Wang this guy, quietly find a girlfriend also don't bring me to see, another day must see real person .... The 22nd morning, the product manager also raised a new demand, to allow users to choose to use Qq\weibo\weixin certification, at this time, you have a deep understanding of the decoration of various loading techniques, easy to achieve new needs.

With parametric adorner
#_ *_coding:utf-8_*_user_status = False #用户登录了就把这个改成Truedef Login (auth_type): #把要执行的模块从这里传进来 def auth (func): def                Inner (*args,**kwargs): #再定义一层函数 if Auth_type = = "QQ": _username = "Alex" #假装这是DB里存的用户信息                    _password = "abc!23" #假装这是DB里存的用户信息 global user_status if user_status = = False: Username = input ("User:") password = input ("pasword:") if username = = _  Username and Password = = _password:print ("Welcome login ....") User_status =                True else:print ("Wrong username or password!")                if User_status = = True:return func (*args,**kwargs) # Look here, as long as the validation passed, call the corresponding function else: Print ("Only support QQ") return inner #用户调用login时, will only return the memory address of inner, the next time when the addition of () will be executed inner function return authdef hom E (): Print ("---home----") @login (' QQ ') deF America (): #login () #执行前加上验证 print ("----euro zone----") def Japan (): Print ("----Japan-Korea zone----") @login (' Weibo ') def Henan (St YLE): ":p Aram Style: What type of love to see, it comes in: return:" #login () #执行前加上验证 print ("----Henan zone----") Home () # Americ A = Login (America) #你在这里相当于把america这个函数替换了 #henan = login (Henan) # #那用户调用时依然写america () # Henan ("3p")

One: Write 3 functions, each function execution time is not the same,

Tip: You can use Time.sleep (2) to let the program sleep 2s or more,

Two: Write the adorner, add the function of statistic run time to each function

Hint: Add Start=time.time () to record the current execution timestamp at the start of the function execution, and at the end of the function execution at Time.time ()-start to take time to execute

Three: To write the adorner, for the function to add the function of authentication, that is, require authentication successfully before executing function

Four: To write the adorner, for a number of functions and authentication function (the user's account password from the file), required to log on successfully once, subsequent functions do not need to enter the user name and password

Tip: Reading a dictionary from a file as a string can be converted to a dictionary format using eval (' {' name ': ' Egon ', ' Password ': ' 123 '} ')

Python's Way function advanced

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: 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.