I. Function Object
A function is a first class object: A value pointed to by a function name can be used as data.
1. Functions can be referenced
For example:
2. Can be passed as a parameter to another function
For example:
3. Can be used as the return value of a function
For example:
4. Elements that can be used as container types
For example:
Two. Function nesting
Function nesting is divided into two main categories
1. Call nesting of functions: in the process of invoking a function, its internal code calls another function
For example:
2. Nested definitions of functions: Define other functions within a function
Three. Namespaces and scopes
Name space:
A namespace is an internal address that holds the name and value of the memory address binding relationship, and whenever the lookup value must be by name, access to the name has to look for the namespace
Namespaces fall into three main categories:
Built-in namespaces: Store the Python interpreter's own name, such as: Len,max,print ...
Global namespaces: File-level names (all names without indentation level)
Local namespaces: Names defined within a function
The life cycle of a namespace:
Built-in namespaces: takes effect when the interpreter starts and the interpreter shuts down
Global namespaces: Effective when the interpreter interprets execution of Python and expires when the file finishes executing
Local namespace: The function's local namespace is temporarily generated only when the function is called, and the function call is complete.
Load Order of namespaces:
Built-in namespaces----> Global Namespaces----> local namespaces
To find the order of namespaces:
Look up based on the current location
Assume the current local namespace, look in order: Local namespace----> global namespace----> built-in namespaces
Scope
Domain: Refers to a range of scopes, which are divided into two types of
Global scope: Contains names in built-in namespaces and global namespaces
Features: Globally valid, global survival
Local scope: The name in a local local namespace
Features: Locally effective, temporary survival
Python function (ii)