Python function advanced

Source: Internet
Author: User

1. Namespaces
1. Global namespace. When a variable outside the function is stored, the relationship (ID) of the variable name and value is stored in memory, which is called the global namespace.

2. Local namespaces. function, he will open a temporary civil space in memory, storing all variables and values in the function body.
As the function executes, the temporary space is closed and the function is used to open up the local namespace

3. Built-in namespaces, where all built-in functions are stored

Load order of three spaces:
Built-in namespace---"Global namespace" (when the function executes) loads the local namespace
Order of Values:
Local namespace--"global namespace---" Built-in load space (when function executes)

4. Global scope: Contains the global namespace, the variable effective scope of the built-in space
Local scope: The scope of the variables that are valid within the function body when the function is opened

2. Nesting of functions:
The relationship between a global variable and a local variable should be taken into account when a function is nested in a function.
def OK ():    def RK ():        print (' 22222 ')    print (' 11111 ')    return rk ()   #当没有return rk () The function inside the function does not execute

Ok ()
##############################################################
def OK ():
Def LK ():
Def PL ():
Print (' Nihao ')
Return PL ()
Print (LK ())
  
Output Result:
Nihao
None
None
  



3.globals () #全局名称空间: Querying all global variables
Locals () #局部名称空间: Querying all local variables


4. Keywords:
Global:1. Within a local space, declare a global variable
2. Change a global variable within a local space

Nonlocal:1. Make global variables non-modifiable
2. Ability to modify local variables, in local variables, modify all of the variables in one

Python function advanced

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.