A little more understanding of the basics of Python

Source: Internet
Author: User

1. Namespaces

Namespaces are a piece of memory space that holds the variable name and value relationship records, with a total of three namespaces in Python, built-in namespaces, global namespaces, and local namespaces. The three namespaces are loaded in the following order,

The Python interpreter runtime built-in namespaces are loaded into memory, and then when the Python program is run, the global namespace is loaded into memory, the variables in the global namespace are loaded from top to bottom, and finally when the function in the program is called, the local namespace is loaded into memory.

The order of the values within three namespaces is the local namespace---the global namespace----built-in namespaces, which are the same in both calling and calling functions.

2. Scope

A scope is a range of variables or functions that can be called in a program, mainly by global scope and local scope. Where global scopes include built-in namespaces, global namespaces, variables or functions defined in built-in namespaces and global namespaces can be called anywhere in the program, local scopes include local namespaces, variables or functions defined in the local namespace can only be called in the current block of code. cannot be called outside of a block of code. In particular, the difference between the built-in global and local methods that are called in the globals and the locals is mainly different from the local method, and the result of the global method being called is to display all the variables or function names in the program. The local method is called in the local scope to display the variable and function name locally, and the global scope is called to display the global variables and function names. There is also a global keyword that is used to allow local variables to be called in the global.

3. Function nesting and scope chain

Function nesting is the definition of a function inside a function, in which a concept of a scope chain is created, in which the intrinsic function can invoke variables of the outer function. For example, the following nested function, the result is output 1.

def f1 ():

    A = 1    def f2 ():        print (a)    F2 () F1 ()


4, the essence of function name
A function name is actually a memory address that points to the body of a function, which, like a variable, can be referenced, as a function's parameter or return value, as an element of a container type. A function name is also the first class of objects that can be created at run time by a program, can be used as a parameter or return value of a function, and can be assigned to a variable.

5. Closed Package
A closure refers to a function, a function that is nested within another function and calls a variable of the outer function. The function of the closure function is to allow the variables called by the outer function to be stored in memory for a long time until the internal function is executed.

A little more understanding of the basics of Python

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.