Python --- --- function parameters share experiences

#### Defined Functions
`` Python `
#-defined functions
DEF function ():
    Print (" the Hello world ")
# call the function
function ()
# Output of
the Hello world
`` `
#### function parameters
`` `Python
#. 1 Function Parameter Type: arguments, parameters, and non-parametric
# 2 arguments: the function call when the parameter arguments can pass any type of object
# 3 parameter: Parameter name, if the function definition It has no real meaning
function DEF (Self):
    Print ( "% S" Self%)
function ( "Hello World")
# "Hello World" as arguments, self parameter is
# Output of
the Hello world
`` `
#### Parameter Type
##### Location parameter
Python `` `
# positional parameters. 1: passing parameters to the function is called according to the parameters defined by the position of the function
# 2 actual parameters passed when calling function of the number and location must be consistent with the definition of the function.
# 3 and arguments. amount must be consistent shape parameter
result=0
def function(a,b):
    result = a + b
    print(result)
function(10,20)  
# Output
30
`` `
##### keyword arguments
Python `` `
#. 1. of the order to transfer the parameter definition, is directly transmitted to the parameter based on the parameter name
# 2. Keyword parameters can be sorted in a different order
# 3. arguments and parameters must match the amount of
result=0
def function(a,b,c):
    result = a + b + c
    print(result)
function(c=10,a=5,b=15)
# Output
30
`` `
##### default parameters
`` Python `
# 1. provide default values for the parameters for a function call can not pass by value of the default parameters, passing parameters will replace the default value
after # 2. The default value of the parameter must be placed no default parameters
DEF function (name, Sex = "boy"):
 Print ( "name is:% s, sex is:% S"% (name, Sex))
function ( "zahng") # no specified sex, will use the default boy
function ( "zhang", "girl ") # specify the sex, with the specified parameters girl
# The output
name is: zahng, gender: boy
names are: zhang, gender: Girl
`` `
##### variable length parameter (* args)
Python `` `
#. 1. In the definition of the function, can add a *, so that the parameter will get to all the arguments (position parameter) at the front parameter (args be other str, usually * args)
# 2. argument all saved to a tuple
# 3. with * only one parameter, and other parameters may be used in conjunction with, can receive position parameters, can not receive the keyword parameter
# 4 undefined long parameters (* args) does not have to write in the end, all the parameters Ruoguo written before the other parameter, variable length parameter (* args) later, the keyword arguments must follow behind the positional parameters, passed as keyword arguments
# 5 If you write directly at the beginning of a formal parameter *, requires all our arguments must be passed as keyword arguments.
DEF function (a, b, * args):
 Print (a, b, args)
function ( 8, 9)
# output
23 (4, 5, 6, 7, 8, 9)
function DEF (* args, A, B):
 Print (A, B, args)
function (4,5,6,7,8,9, A = 2, B =. 3) A #, B parameter must be keyword parameters passed
# output
23 (4, 5, 6, 7, 8, 9)
Beginning direct parameter * (* no arguments)
DEF function (*, A, B):
 Print (A, B)
function (A = 2, B =. 3)
# output
2,3
`
##### variable length parameter (** kwargs)
Python `` `
#. 1. In the definition of the function can be added in 2 ** front parameter, so that this parameter will obtain all the keywords (args be other str, usually args *)
# 2 ** parameter can receive keyword arguments, it will unify these parameters are saved to a dictionary, the dictionary key is the name of the parameter, the dictionary value is the value of the parameter
# 3. ** only one parameter, and must be written in the last of all parameters
def function(a,b,**kwargs):
    print(a,b,kwargs)
function(a=1,b="qaq",c=4,d="qwer",e=5)
# Output
. 1 QAQ { 'C':. 4, 'D': 'qwer', 'E':. 5}
`` `
##### parameter combinations
Python `` `
# sequential positions: a position parameter, default parameters, keyword parameters, a variable length parameter
DEF function (A, B = 10, * args, ** kwargs):
    Print (A, B, args, kwargs)
function ( 2,12,32, "AA", name = "Kimi", Age =. 8)
`` `
 

Guess you like

Origin www.cnblogs.com/niaocaizhou/p/12015273.html