Knight Plan-python full stack 10 function advanced

Source: Internet
Author: User

Dynamic parameters of the function (universal parameters) *args **kwargs

In order to expand, the number of arguments passed in is not fixed, and the created

*args usage

At the time of the function definition, the *args aggregation, which aggregates the positional parameters of all the arguments to a tuple, assigns the tuple to args.

def fuc1 (a,*args):    print(a)    print(args) fuc1 ( 1,2,3,4,5,6,7)        #1      (2, 3, 4, 5, 6, 7)           

At the time of the function definition, the **kwargs Poly Dictionary, which aggregates all the key parameters of the argument into a dictionary key-value pair

def func1 (a,**Kwargs):    print(a)    print" Taibai " " Alex " " male " #taibai#{' num ': ' Alex ', ' age ': +, ' sex ': ' Man '}
* The magic of Use

When the function is defined, the aggregation is represented after the position

Break in position after the function is executed (easy to understand)

L1 =[= = [111,22,33,'Alex']def func1 (*args):     print(args) func1 (*l1,*l2)  #(1, 2, 3, 111, +, ' Alex ') Can be understood as breaking up before aggregating into a tuple within a function
#同理, * * Dictionaries can also be broken down and then aggregated

Formal parameter position priority

Position parameters on the leftmost, followed by *args, followed by the default parameters, and finally **kwargs, there are generally only one or two operations, there are almost no four all in the case

name Space
Let's start by remembering what the Python code does when it runs, starting with the Python interpreter, and opening up a space in memory whenever a variable is encountered.
The corresponding relationship between the variable name and value is recorded, but when a function definition is encountered, the interpreter simply reads the function name as memory, indicating that the function exists.
As for the variables and logic inside the function, the interpreter does not care at all. When executing to a function call, the Python interpreter will open up a memory to store the contents of the function, and at this time, focus on what variables are in the function,
The variables in the function are stored back in the newly opened memory, and the variables in the function can only be used inside the function, and all the contents of this memory will be emptied as the function executes. We gave the space for the ' relationship of name and value ' to a name -------namespace . At the beginning of the code, the space created to store "the relationship between the variable name and the value" is called the global namespace; The temporary space opened in the function's run is called the local namespace.

Global namespaces: is a container for information that stores variable names and variable values corresponding to addressesLocal namespaces: The space that is temporarily opened up in a function run. (Temporary presence)Built-in namespaces: function scope global scope defined internally in Python: Includes global namespace and built-in namespace local scope: local namespace (temporary, disappears after call completes)

Order of values: the nearest principle, from the local name space to find, and then to the global namespace to find, and finally in the built-in function space to find

Load order: Built-in namespace---global namespace (when program executes)---Local namespace (when a function is called)

Nesting of functions: Take a step-by-step look, don't be careless

That is, nesting functions inside a function

#x=1#def f1 ():#def f2 ():#print (Locals ())#print (x)#print (Locals ())#return F2#x=100#def f3 (func):#x=2#print (Locals ())#func ()#x=10000#f3 (F1 ())#print (x) #最后输出结果为10000
Keyword: global nonlocalglobal

1. Declaring a global variable

2. When a local scope wants to make modifications to global variables of global scope, it is necessary to use the global

Note: For variable data types (list,dict,set), you can refer directly without the need to pass the global

L2 = [1,3,4]dic={"a":"b","C":"D"}deffunc1 (): L2.append (6) dic["Q"] = 3Print(DIC)#{' A ': ' B ', ' C ': ' d ', ' Q ': 3}    Print(L2)#[1, 3, 4, 6]func1 ()Print(DIC)#{' A ': ' B ', ' C ': ' d ', ' Q ': 3}Print(L2)#[1, 3, 4, 6]

nonlocal

1. Cannot modify global variables

2. In the local scope, the variables of the parent scope (or the outer, non-global scope ) are referenced and modified, and which layer is used, from which the following variables are all changed

#def func1 ():#B =#def func2 ():#nonlocal b#B + = ten#print (b) #b =#def func3 ():#nonlocal b#B + =#print (b) #b =#func3 ()#Func2 ()#func1 ()
The scope chain of the function:

A small range of scopes can use a wide range of variables, but vice versa, because he is a single

Knight Plan-python full stack 10 function advanced

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.