Function definitions and calls of Python18, comments

 

A function definition

  def function name (parameter 1, parameter 2, ...):

    Function body

    Return Value return (to return anything, a value, a variable, a return value or another function, if the function does not return a value, the return statement may be omitted)

 

Second, the concept of function documentation

 

  As a function of the document, a string of the meaning of the functions and parameters will be described in the Python function in vivo in the first line character string function, the function name may be used to keep the form __doc__ member operator returns the string ready for use who see, in addition to the string can also print directly from the help (function name) form

. 1  DEF function ():
 2      ' This document describes a function string is not defined ' 
. 3      Print ( " It is a simple small program! " )
 . 4  
. 5 >>> Print (function. The __doc__ )
 . 6 that is a function of documentation is not defined strings
View Code
1 >>> DEF function ():
 2      "" " This is a function of documentation is not defined strings " "" 
3      Print ( " It's a simple little program! " )    
 4 >>> Print (function. the __doc__ )
 . 5  which is a function of documentation, not defined strings
 . 6  
. 7  
. 8 >>> DEF function ():
 . 9      " this is the function of documentation, not a definition of the string " 
10      Print ( " it is a simple small program! " )
 11 >>> Print (function.__doc__)
12 This documentation is a function, not a definition of the string
 13 is  
14  
15 >>> DEF function ():
 16      ' which is a function of documentation, not defined strings ' 
. 17      Print ( " ! This is a simple applet " )    
 18 is >>> Print (function. the __doc__ )
 . 19  this is a function of documentation, not defined string
 20 is  
21 is  
22 is >>> DEF function ():
 23 is      "" " it is a function of documentation, not defined strings " "" 
24-      Print ( " it's a simple little program! " )
 25 >>> help(function)
26Function function ON Help in Module1 __main__ :
 27  
28  function ()
 29      which is a function of documentation, not defined strings
 30  
31 is >>> DEF function ():
 32      ' which is a function of documentation, not defined strings ' 
33      Print ( " it's a simple little program! " )
 34 >>> Help (function)
 35 Help function ON function in Module __main__ :
 36  
37  function ()
 38      which is a function of documentation is not defined strings
View Code

Third, keyword arguments

  When the function is defined parameter relatively long time, the argument to the one-parameter function is called, so then we could put the argument and the order parameter of the mess, so, using the keyword parameters can effectively avoid this case, directly to the argument value assigned to the corresponding formal parameters when invoked, this time need not correspond to the order.

  Format: function name (parameter 2 = 2 arguments, parameters, arguments 1 = 1)

1  DEF function (name, Action):
 2      Print ( " My Name:% S " % name)
 3      Print ( " I'll S% " % Action)
 4  
5  Not used keyword arguments:
 6 function ( ' run ' , ' ZZ ' )
 7  my name: run
 8  I would ZZ
 9  
10  keyword parameters:
 11 function (Action = ' run ' , name = ' ZZ ' )
 12  my name: zz
13 I will run
View Code

 

Fourth, the default keyword arguments

  When the function definition, we give parameter initial value, so that when the call is not endowed arguments, the function can also be run

. 1  DEF function (name = ' Kekekeke ' , Action = ' Fly ' ):
 2      Print ( " My name: S% " % name)
 . 3      Print ( " I S% " % Action)
 . 4  
. 5      
. 6 > >> function ()
 7  my name: Kekekeke
 8  I'll Fly
 9 >>> function ( ' ZZ ' , ' eat ' )
 10  my name: ZZ
 11 I will eat
View Code

Fifth, to collect parameters

  1, a function definition collection parameters, one more in front of the parameter * (asterisk) indicates that the parameter is a tuple type variable,;

  2, when the back * not all parameters, the parameters do not need to collect, use keyword specified * parameter will not be collected with the keyword;

  3, when the parameters need to be collected with a keyword, when defining the function can be used ** (binary number) in front of the parameter, the parameters thus collected will be used as a dictionary

 

. 1  DEF function (* name, Action):
 2      Print (name)
 . 3      Print ( " I S% " % Action)
 . 4      
. 5 >>> function (1,2,3,4,5,6, Action = ' Fly ' )
 6 (1, 2, 3, 4, 5, 6 )
 7  I Fly
 . 8 >>> DEF function (** name):
 . 9      Print (name)
 10  
. 11 >>> function (A = ' Kesheng Tao ' , = B ' Kekekeke ' , C = 12306 )
12 is { ' A ' : ' Ke Shengtao ' , ' B ' : ' Kekekeke ' , ' C ' : 12306}
View Code

 

Guess you like

Origin www.cnblogs.com/ksht-wdyx/p/11327904.html