function scopes in Python

Source: Internet
Author: User
Tags variable scope
This article mainly introduces the function scope of Python, has a certain reference value, now share to everyone, the need for friends can refer to

In Python, a function is a scope. This article focuses on the functional scope of Python, interested friends to see it together

In Python, a function is a scope

name = ' Xiaoyafei ' def change_name ():  name = ' Showa '  print (' name in Change_name: ', name) Change_name () # Call function print ( "Outside the name:", name)

The results of the operation are as follows:

Name in Change_name: Showa
On the outside of the Name:xiaoyafei

Let's try again how is it found in nested functions?

Age = 15def func ():  print (' First Age: ', age) # First layer age:15  def func2 (): Age    =    Func2 print ("Age in", age) # Fu age:73    def func3 () in Nc2 (): Age      = all      print ("Age in Func3:", age) # func3 in age:84 func3    () # Call func3 function  Func2 () # Call Func2 function func ()

In the above nested function, it is well explained that a function is a scope, then we can now change the code a little bit to see the situation?

Age = 15def func ():  print (' First layer Age: ', age) # First layer age:15  def func2 ():    print ("Age in Func2:", age) # Func2 in age:15 # See, if there is no age variable in the current scope, then it will look up    def func3 (): Age      =      print ("Age in Func3:", age) # func3 in age:84    func3 () # Call the FUNC3 function  Func2 () # Call the Func2 function func ()

Well, then, when someone says, "a whole bunch of crap is about local variables and global variables, then I want to ask: in this nested function, there is no age variable in Func2, so how does it find the global variable, age = 15?"

Now we need to look at the scope lookup order:

Variable Scope LEGB

    • namespaces within the L:locals function, including local variables and arguments

    • E:enclosing the namespace of the outer nested function, that is, adjacent to the previous layer, for example, said: FUNC2 No age variable will go to the Func find this

    • G:globals Global Variables

    • B:builtins the namespace of the built-in module

Cough, or first understand what is the name space?

namespace, aka name space, as the name implies is the place to store names, what name? for example, x = 1, 1 is stored in memory, so where is the variable name x stored? Namespaces are places where names X and 1 bind.

>>> x = 1>>> ID (1) 1576430608

The namespace is divided into the following 3 types:

    • 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 scope: Temporary inventory, partially valid

Let's take an example to see

Level = ' L0 ' n = 22def func (): Level  = ' L1 '  n = "  print (Locals ()) # {' n ': ' ' Level ': ' L1 '} said in Python, a letter The number is a scope, which perfectly embodies the  def outer ():    n = level    = ' L2 '    print (locals (), N) # {' Level ': ' L2 ', ' n ': +}    EF inner (): Level      = ' L3 '      print (locals (), N) # {' Level ': ' L3 ', ' n ': "Inner}" () ()  outer () func ()

With the rules of L-E-G-->b, that is: in the local can not find, will go to local outside the local search (such as closures), and can not find the global find, and then go to the built-in function to find.

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.