The transfer function parameters python3

A variable object is immutable

  In python, strings, tuples, and the numbers are not subject to change, and list, dict and so it can be modified objects. 

Immutable type: a = 5 Variable Assignment Assignment then a = 10, where int is actually generate a new value object 10, a point let it be discarded and 5, instead of changing the value of a, a is newly equivalent .
Variable Type: Variable Assignment la = [1,2,3,4] la assignment after the third element [2] = 5 to change the value sucked List la, la in itself did not move, only part of the value of its internal It is modified.

python parameter transfer function:

immutable: the similarity value passed c ++, such as integer, string, a tuple. The value Fun (a), only a transfer of a subject itself has no effect. Such modifications (a) an internal value of a fun,
but modify the object copied to another, does not affect a per se.

Variable types: Similar to c ++ is passed by reference, such as lists, dictionaries. The fun (la), pass over the real sucked la, la modified outside fun also affected

two pass immutable objects
1 def ChangeInt( a ):
2     a = 10
3 
4 b = 2
5 ChangeInt(b)
6 print( b )
7 
8 # 2

Third, the object variable transmission

. 1  DEF changeme (mylist):
 2      " Review list incoming " 
. 3      mylist.append ([. 1, 2,. 3,. 4 ])
 . 4      Print ( " the value of the function: " , mylist)
 . 5      return 
. 6  
. 7  # call changeme function 
. 8 mylist = [10, 20, 30 ]
 . 9  changeme (mylist)
 10  Print ( " external function values: " , mylist)
 . 11  
12 is  # the function values: [10, 20, 30, [1, 2, 3 , 4]] 
13  # external function values: [10, 20, 30, [1, 2, 3, 4]]

Fourth, we must parameters

Must parameters: 
   the required parameters to be passed to the function in the correct order. When the number of calls and the same must be declared when. 
   Call printme () function, you must pass in a parameter, or will a syntax error:
# Error demonstration 
DEF printme (str):
     " Print any incoming string " 
    Print (str)
     return 

# calls printme function 
printme () 

# TypeError: printme () 1 Missing required Positional argument: 'str'

Fifth, keyword arguments

Keyword arguments: 
   key parameters and function calls the close relationship between the function call using keywords to determine the parameters passed in parameter values. 
   When you use keyword parameters and allows the order parameter of the function call statement is inconsistent, because the Python interpreter to match the parameter value with the parameter name. 
   The following example uses the parameter name when you call the function printme ():
# Examples 1: 
DEF printme (str):
     " Print any incoming string " 
    Print (str)
     return 

# calls printme function 
printme (str = " Geng Yufei " ) 

# Geng Yufei 

# Example 2: 
# The following examples demonstrate the function parameters does not require the use of specified order: 

DEF printinfo (name, Age):
     " Print any incoming string " 
    Print ( " name: " , name)
     Print ( " Age: " , Age)
     return 


# calls printinfo function 
printinfo ( = 50 Age, name = "Geng Yufei " ) 

# Name: Geng Yufei 
# Age: 50

Sixth, the default parameters

Default parameters: 
   the function is called, if no arguments are passed, it will use the default parameters. The following examples if no incoming age parameter, the default value:
DEF printinfo (name, Age = 35 ):
     " Print any incoming string " 
    Print ( " name: " , name)
     Print ( " Age: " , Age)
     return 
# calls printinfo function 
printinfo (age = 50, name = " Geng Yufei " )
 Print ( " ------------------------ " ) 
the PrintInfo (name = " Geng Yufei " ) 

# name: Geng Yufei 
# Age: 50 
# ------------------------ 
# name: Geng Yufei
# Age: 35

Seven single asterisk Ganso parameter

Variable-length parameters: 
   You may need a function that can handle more parameters than the original statement. These parameters, called variable length parameters, and the above three different parameters, not naming declaration. 
   Plus an asterisk * parameter will be introduced as a tuple (tuple), and storage of all variables unnamed parameters.
DEF printinfo (arg1, * vartuple):
     " Print any incoming parameter " 
    Print ( " Output: " )
     Print (arg1)
     Print (vartuple) 

# call printinfo function 
printinfo (70, 60, 50 ) 

# Output: 
# 70 
# (60, 50) 

# example 2: If no arguments are specified in the function call, it is a null set. We can not pass variables to unnamed function. Following examples: 

DEF PrintInfo (arg1, * vartuple):
     " Print any incoming parameters " 
    Print ( " Output: " )
     Print (arg1)
     forvar in vartuple:
         Print (var)
     return 

# call function PrintInfo 
PrintInfo (10 ) 
PrintInfo ( 70, 60, 50 ) 

# Output: 
# 10 
# outputs: 
# 70 
# 60 
# 50

Eight double asterisk dictionary parameter

Parameters: ** 2 plus, introduced in the form of a dictionary
. 1  DEF PrintInfo (arg1, ** vardict):
 2      " print any parameters passed " 
. 3      Print ( " Output: " )
 . 4      Print (arg1)
 . 5      Print (vardict)
 . 6  
. 7  
. 8  # call PrintInfo function 
. 9 PrintInfo (. 1, 2 = A, B =. 3 )
 10  
. 11  # output: 
12 is  # . 1 
13 is  # { 'A': 2, 'B':. 3}

Nine, the asterisk parameters:

Precautions:

1  # function is declared parameter asterisk * may appear alone, for example: 
2  
. 3  DEF F (A, B, * , C):
 . 4      return A + B + C
 . 5  
. 6  # If the asterisk * separate the parameters must use keywords to pass, otherwise it will error. 
. 7  
. 8  DEF F (A, B, * , C):
 . 9      return A + B + C
 10  
. 11 F (l, 2,3)    # error 
12 is  
13 is  # --------------- -------------------------------------------------- -------- 
14  # Traceback (MOST Recent Last Call): 
15  #    File "<stdin>", Line. 1, in <Module1> 
16  # TypeError: f() takes 2 positional arguments but 3 were given
17 
18 def f(a,b,*,c):
19     return a+b+c
20 
21 f(1,2,c=3) # 正常
22 # -----------------------------------
23 # 6

 

Guess you like

Origin www.cnblogs.com/gengyufei/p/11317085.html