Python study notes - arguments

Directly look at the code, the code which has illustrated and described, it is desirable to understand the function parameters

. 1  # ! / Usr / bin / to python3 
2  # - * - Coding: UTF-8 - * - 
. 3  '' ' 
. 4  the Author: flyinghappy
 . 5  a Date: 2020.02.14
 . 6  Note: function
 7  ' '' 
8  '' ' function parameter '' ' 
9  ' '' 
10  in Python defined functions, you can be a mandatory parameter, default parameter, the variable parameter, and named keyword keyword parameter
 11  five types of parameters may be used in combination. Note, however, the order of the parameters must be defined:
 12  mandatory parameters, default parameters, variable parameter, named keyword arguments and keyword arguments.
13 is  ' '' 
14  DEF function (required_parameters, default_parameters = ' the I AM default_parameters ' ,
 15     args *, named_key_parameters, ** kwargs):
 16      Print ( ' which is mandatory parameters: ' , required_parameters)
 . 17      Print ( ' this is the default parameters: ' , default_parameters)
 18 is      Print ( ' which is a variable parameter: ' , args )
 . 19      Print ( ' which is named key parameters: ' , named_key_parameters)
 20 is      Print ( ' which is a keyword parameters: ' , kwargs)
 21 is  
22 is  IF  the __name__ == ' __main__ ' :
 23 is 
24      required_parameters = ' mandatory parameters ' 
25      default_parameters = ' the I AM default_parameters ' 
26 is      args = [ ' which is a variable parameter ' , ' which is a variable parameter ' , ' which is a variable parameter ' ]
 27      named_key_parameters = ' This is keyword parameter named ' 
28      kwargs = { ' name ' : ' flyinghappy ' , ' Age ' : 42 is }
 29      ' ''Parameters passed according to the format '' '
30      function (required_parameters, default_parameters,
 31 is          * args, named_key_parameters = named_key_parameters, ** kwargs)
 32      Print ( ' --------------------------- ------------------------------------------- ' )
 33      ' '' simple sequentially pass parameters ' '' 
34 is      function ( ' mandatory parameter ' , ' the I AM default_parameters ' ,
 35          ' which is a variable parameter ' , ' which is a variable parameter ' , ' which is a variable parameter ' ,named_key_parameters='This is a key parameter named ' , name = ' flyinghappy ' , Age = 42 is )
 36      Print ( ' ---------------------------- ------------------------------------------ ' )
 37      ' '' by list incoming and dictionaries, attention must be * and ** '' ' 
38 is      function (* [ ' mandatory parameter ' , ' the I AM default_parameters ' ,
 39          ' which is a variable parameter ' , ' which is a variable parameter ' , ' this is a variable parameter ' ], named_key_parameters = 'It is named keyword arguments ', ** { ' name ' : ' flyinghappy ' , ' Age ' : 42 is })
 40      '' ' three operating the same way as the above results incoming! '' '
View Code

Code above results are as follows:

 

Guess you like

Origin www.cnblogs.com/flyinghappy/p/12376622.html