Python Foundation Day

Source: Internet
Author: User

Today's main content

1. Dynamic Parameters

2. Namespace or name space

3. Nesting of functions

4. Keywords Global and nonlocal

Dynamic functions

Dynamic Parameters *args, **kwargs

(1) *args

Receives all positional parameters corresponding to the argument (the argument is outside the formal parameter position) and places it in a tuple.

Example 1.*args position correctly placed (without default parameters)

Example 2: With default parameters, does not affect the value of default parameters

Summarize:

Formal parameter Correspondence order: Position parameter, *args, default parameter

(2) **kwargs Dynamic transfer parameters

Add all keyword parameters (keyword parameters that exceed parameter settings) to a dictionary.

Example 3:**kwargs correct use

Final Order of arrangement:

Example 4:

Summarize:

Final order: Positional parameters, *args, default parameters, **kwargs

How to write a general job:

def funcl (*args,**kwargs):

Pass

Funcl

Knowledge Points: * Application

Example of the role of 5:*args,*

Convert the data types of each iteration into a Narimoto group form, formerly a tuple, directly applying the primitive

The role of 6:**kwargs in example

Summary *,**

When the function's call executes, the

* The object can be iterated, representing the list,tuple,str,dict (key) to add element one by one to args.

* * Dictionary, the representative of the broken, put all the key-value pairs into a Kwargs dictionary.

When the function is defined, *args,**kwargs represents the aggregation.

def func (*args,**kwargs): # Aggregation
Print (args)
Print (Kwargs)

Func (*,**) #打散

=================================================================

2. namespace, name space

The namespaces are divided into three types:

Global namespaces

Local namespaces

Built-in namespaces

* The built-in namespace holds the Python interpreter's name for us: Input,print,str,list,tuple ... They are all familiar to us and can be used in a way.

Order of loading and taking values between three namespaces:

load Order: built-in namespaces (pre-Program load)-Global namespaces (program run: Load from top to bottom) local namespaces (program run: Load only when called)

Order of Values:

Built-in namespaces, local namespaces, and global namespaces, local call spaces

Global invocation: Built-in namespaces, global namespaces

To sum up, in the search for variables, from the small range, one layer to a wide range to find.

As a simple description:

Load as a downward arrow, with a value of up arrow.

Scope

Scope is the scope of action, according to the effective scope can be divided into global scope and local scope.

Global scope: Contains built-in namespaces, global namespaces , can be referenced anywhere throughout the file, are globally valid

Local scope: Local namespace, only valid locally

Globals and Locals methods

Example 7: Comparing global and local scope differences

The values are all in the dictionary.

Global namespaces: All variables built into the PY3

Local namespaces: The variables are displayed according to their location, and if there are variables in the function, the variables of the function are plotted, and if they are empty, only the current level is found.

Contrast

Nesting of functions

Solution Ideas:

1. First confirm what the function does, do not see the calling function, then the function does not execute. That is, the function name ()

2. The code executes sequentially from top to bottom.

3. Call function: function inside from top to bottom execution

Example 8: By using an example to understand the above, it is good to understand

4 keywords: Global and nonlocal (declaring variables)

Global

1, within the local space, declare a global variable.

2, Global (limited to string, number) is required when local scope wants to make a modification to a globally scoped global variable.

Example 9:

If the global declaration is not used, the internal variables cannot be found externally.

If declared, this variable can be used externally

2, change a global variable within the local space

Example 10:

Nonlocal #将内部变量, referencing to external, excluding global

1, global variables cannot be modified.

2. In a local scope, references and modifications are made to variables of the parent scope (or the outer scope non-global scope).

and which layer of the reference, from that layer and the following this variable all changed. (as long as within the function, you can refer to the outer layer, as long as the function, to the global error)

Summarize:

Global and nonlocal usage are the same, nonlocal cannot be used globally, and global can be used from local to global.

Python Foundation Day

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.