Python learns day, namespace and scope, nested functions, Global and nonlocal

Source: Internet
Author: User

Name space:

Built-in namespaces: When opening pytharm is loaded

Global namespaces: When running a py file is loaded

Local namespace (temporary namespace): loaded when called in a py file

def func ():-----> Function definition

Pass-------> function body

Func ()-------------> function call

  

function definition: Only the corresponding relationship of function name is stored in memory, and the contents of function body are not stored

Function call: Executes the contents of the function body, creates a temporary space, and as the function executes, the contents of the temporary namespace in memory are emptied

Scope:

Global scope: Content in the built-in namespace + content in the global namespace

Local scope: Local namespace

Order of Values:

Local---> Global---> Built-in, one-way value, irreversible.

A=10b=20def Test5 (A, b):    print (A, b)---> 20,10    a=3    b=5    print (A, b)---> 3,5c = TEST5 (b,a) print (c) ---------> None

Load Order:

Built-in namespaces

Global namespaces

Local name space

Nesting of functions:

See the call on the Execute

Global and nonlocal (when the data type is immutable, you need to declare it, but when the data type is a list, you can add data directly to the list using append and directly cause changes to the global variables)

Global: Declare that the elements within a function can be global variables (functions can directly refer to global variables, no global variables are not changed by the function)

Nonlocal: Declares that a child function can change a variable of a parent function, but nonlocal cannot change a global variable (a child function can directly reference a variable of a parent function, and no nonlocal cannot change the parent function)

Count = 1def func1 ():    count + = 1 # will error # count = 2 will not error    
Def func1 ():    count = 666    def inner ():        print (count)        def func2 ():            nonlocal Count            # Global count< C6/>count + = 1 print ('            func2 ', count)        Func2 ()        print (' inner ', count)    inner ()    print (' func1 ', Count) func1 () print (count)

Python learns day, namespace and scope, nested functions, Global and nonlocal

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.