Advanced Python function

A dynamic parameter of the function

Before we said that the mass participation, if we need to pass parameters to a function, and the parameter is uncertain. Or I pass a lot of parameters to a function, the parameter I would write a lot, a lot of trouble, how to do it. we can consider the use of dynamic parameters.

Dynamic parameters of two types:

1. * args dynamic parameter receiving position

When receiving dynamic parameters to be noted: dynamic parameters must follow the position parameters

. 1  DEF Xue (* Language, ' A ' , ' B ' ):
 2  
. 3    Print ( 'I want to learn:', Language, A , B )
 . 4  
. 5 Xue ( ' the PHP ' , the JAVA, ' the Python ' )

If the program does not follow the order will be given, then, as the above code, because the previous pass in all positions of the received parameters gave * language, a and b can not never received parameters

 

 

 It must be rewritten into the following code:

. 1  # - * - Coding: UTF-. 8 - * - 
2  DEF Xue (* Language, A, B):
 . 3      Print ( ' I want to learn: ' , Language, A, B)
 . 4 Xue ( ' JC ' , ' the PHP ' , A = ' Java ' , B = ' the Python ' ) # parameters must be specified keywords

 

 

 This time a and b will have value, but write it position parameters can not be used. So we must first write the positional parameters, and then use dynamic parameters

. 1  # - * - Coding: UTF-. 8 - * - 
2  DEF Xue (A, B, * Language):
 . 3      Print ( ' I want to learn: ' , A, B, Language)
 . 4 Xue ( ' JC ' , ' the PHP ' , ' Java ' , ' the Python ' )      # the first two parameters used to receive the location parameters, parameters of the back receiving dynamic parameters

The default value of the parameter which it?

This time we found all the default values ​​are born. This time if not given key parameter passing. So your default is always in effect.

Order to keep in mind: location parameters, dynamic parameters * The default value of the parameter

2. ** kwargs receiving dynamic keyword arguments

 

The parameters can be dynamically position in python, but this situation can only receive location * parameter ⽆ not receive keyword arguments.
To receive dynamic keyword parameters ** Use manipulation in python

1 def xue(**kwargs):
2     print(kwargs)
3 xue(a='Python',b='PHP',c='vb')

 

 

 This time received are dict (dictionary)

The order of questions, at the time a function call, if the first keyword arguments are given, the entire function will error

. 1  DEF Xue (A, B, C, D):
 2      Print (A, B, C, D)
 . 3  # key parameters must precede position parameter, or parameter being given to the confusion 
. 4 Xue (1,2, C = ' the Python ' , 666)

It must keyword parameter. Since the argument that order. Therefore, when the parameter received is in the back position parameters which
sequential That position parameter must ⾯ surface before the keyword parameters. Receives key parameters also in the dynamic behind the
final sequence (*):
position parameter> * args> default parameters> ** kwargs

Function Note:

. 1  DEF XUE (the JAVA, PHP):
 2      "" " 
. 3      This function is used to summarize the programming language learning,
 . 4      : param the JAVA: Parameter Java language is Java
 . 5      : param PHP: PHP is PHP language parameter
 . 6      : return: return What is the
 7      "" " 
8      Print (JAVA, PHP)
 9      return   ' stick '

II. Namespaces

After the python interpreter starts executing, it will open up a space in memory, whenever it encounters a variable, put the variable relationship between the name and the value recorded, but when faced with the function definition, the interpreter just the function name is read into memory, indicating that the function exists, as to the internal logic of variables and functions, the interpreter is not concerned about. that is the beginning of time function just loaded in, nothing more, and only when the function is called time of the visit, the interpreter will open up space for internal variables according to the variables declared within a function. with the completion of function execution line, these functions are internal variables as a function of the space occupied will be emptied finished

. 1  DEF Xue ():
 2      A = ' YJ ' 
. 3      Print (A)
 . 4  Xue ()
 . 5  Print (A)     # A does not exist already

 

1. built-in namespace: Store python interpreter provides us with the name, list, tuple, str, int these are built namespaces

2. The global name space: We py files directly in the file, the variable declared outside the function belong to the global namespace

3. The local name space: the amount of variables declared in the function will be placed on local namespace

Scope:

1. The global scope: the built-in Global +

2. Local scope: local (function is called)

III. Nested functions

Each function can be nested

1. As long as met () function is called, if no () function call is not

 

2. The order of execution of the function

 

Four key global and nonlocal

global: local access to global content in

. 1 A = 10 # global variable itself is insecure and can not modify, closure 
2  DEF FUNC ():
 . 3      Global A   # 1. the content can be introduced into the interior of the global functions, 2. Create a global variable 
4      # A = 20 is 
. 5      A + 10 = # A + A = 10 
. 6      Print (A)
 . 7  FUNC ()
 . 8  Print (A)
 . 9 A 10 = 
# result, 20,20

nonlocal: find the nearest outer function in that local variables

. 1 a = 10
 2  DEF Outer ():
 . 3      a = ' YJ ' 
. 4      DEF inner (): # changing the value of a in the inner 
. 5          nonlocal a # Looking for the nearest layer function that variable 
. 6          a = 20 is
 . 7          Print (A)
 . 8      Inner ()
 . 9  Outer ()
 10  Print (A)

Guess you like

Origin www.cnblogs.com/yjtxin/p/11817092.html