Python-based namespaces and scopes, function nesting

Source: Internet
Author: User

First, the name space

1, definition: storage of the name and value of the binding relationship

2. Namespaces are divided into: built-in namespaces, global namespaces, local namespaces

Built-in namespaces: The Python interpreter comes with its own name, and the Python interpreter starts to generate

Global namespaces: File-level-defined names are stored with the global namespace, which is generated when a Python file executes

Local namespaces: Define names inside functions, local namespaces only take effect when functions are called, and end of function calls are invalidated

3. Order of loading: Load order of the three: built-in namespaces--global namespace--local namespace

4. Order of values: Built-in namespace, global namespace, local namespace 7

## max=10#def func (): #x =1## max=20#print (max)###func ()#max=10##def func (): #x =1#max=20## Print (max)#func ()###print (max)### x=0#def f1 ():## x=1#def f2 ():## x=2#def f3 ():## x=3#print (x)#f3 ()#F2 ()##F1 ()

Second, scope

1. Scope:

Global scope: The name of the built-in namespace and the global namespace are global, can be referenced anywhere in the entire file, and are globally valid

Local scope: The name of the local namespace is local, and can only be referenced within the function, locally valid

#x=1#def foo ():#def f2 ():#print (x)#F2 ()#def Bar ():#print (x)##foo ()#Bar ()#def f1 ():#x=1#def f2 (): #f2 =value## x=2#print (x)#F2 ()#F1 ()x=1deffunc (): x=2defF1 ():Pass    #Print (dir (Globals () [' __builtins__ ']) #全局作用域name    #print (Locals ()) #局部作用域namefunc ()Print(Globals () isLocals ())

2, the action sequence is: local scope "" "" The global scope

Three, function nesting

1. Nested Definitions of functions

 def   F1 ():  def   F2 ():  def   F3 ():  print  ( " from f3  "  )  print  ("  from f2   " " F3 ()  print  ( " 
   
    from F1  
     " 
    ) F2 () 
     #   print (F1)  F1 () 
   

2. Nested calls to functions

def max2 (x, y     ): if x > y:        return  x     Else:        return y def max4 (a,b,c,d):    res1#    #  at    #  to    return Res3 Print (Max4 (11,23,-7,31))

Python-based namespaces and scopes, function nesting

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.