Python function (ii)

Source: Internet
Author: User

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)

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.