Parameter definition:
1. Position parameters:
This is a familiar normalization parameter, the positional parameters must be passed in the exact order defined in the calling function, and in the case of no default arguments, the incoming parameters
The exact number must be the same as the number of declarations.
def foo (who, name):
Print (' {0} is {1} '. The format (who, name)
Foo function has two positional arguments, which means that the call to Foo must have two arguments, no more or less
2. Default parameter:
defines the default parameters as follows:
def func (Posargs, Defargs=dval, Defargs2=dval2,...):
' function_document_string '
Unction_body_suite
No default parameter is followed by a default assignment statement, and if the value is not passed to the default parameter in the call to the function, the function uses that assignment to implement the argument invocation.
3. Variable length parameters:
is significantly different from regular functions if you need to handle a variable number of arguments during a call to a function, because the number of arguments is unknown before the call to the function (even during run time). The number of call parameters for each function is also different) this use requires the use of variable length parameters to define functions.
Non-keyword variable length parameter (tuple)
def func ([formal_args],*args_tuple):
' function_document_string '
Function_body_sui The shape arguments after the Te
asterisk operator is passed as a tuple to the function. The tuple holds all the extra arguments passed in.
def tuplevarargs (arg1,arg2= ' DEFAULTB ', *therest):
Print (' Formal arg 1: ', arg1)
Print (' Formal arg 2: ', ARG2)
for Eachxtarg in therest:
print (' Another args: ', eachxtarg)
Keyword variable argument (dictionary)
Use XX in order to differentiate between keyword and non-keyword parameters
def func ([Formal_args], [*vargst], [**VARGSD]):
' Function_document_string '
Function_body_suite
The arguments passed in are paired, and the key in the dictionary is the parameter value corresponding to the argument name value.
1 defNewfoo (FORMALARG1, formalarg2='default', *nnameargs, * *Nameargs):2 Print('Formal 1:', FORMALARG1)3 Print('Formal 2:', FORMALARG2)4 forNnameinchNnameargs:5 Print('no key name args:', Nname)6 forKnameinchNameargs.keys ():7 Print('Key name args {0}: {1}'. Format (Kname, Nameargs[kname]))8 9 #normal way to call functionsTenNewfoo (Ten, A, a, fifty=50, sixty=60) One A #pass non-keyword parameters and keyword arguments by x and xx -Newfoo (10, 20, * (30, 40), **{'Fifty': 50,'Sixty': 60}) - the #to construct tuples and dictionaries for parameter passing outside of a function -Atuple = (30, 40) -Adict = {'Fifty': 50,'Sixty': 60} -Newfoo (Ten, *atuple, **adict)
View Code
Pass parameters:
In fact, the parameters can be passed in the form of the definition of function parameters of the inverse derivation to pass
1 defAdd (A, b):2 returnA +b3 4 5 Print(Add (1, 2))6 Print(Add (b=2, a=1))7 Print(Add (* (1, 2)))8 Print(Add (**{'a': 1,'b': 2}))
View Code
parameter definition and invocation of Python function