This article summarizes the way Python passes parameters. Share to everyone for your reference. The specific analysis is as follows:
When a formal parameter such as *arg represents an incoming array, the incoming dictionary is represented when the formal parameter is **args.
def myprint (*commends,**map): For Comm in commends: print comm for key in Map.keys (): print Key,map[key ] myprint ("Hello", "word", username= "Tian", Name= "Wei")
Output:
Hellowordusername Tianname Wei
Python defines a function that can be called by a normal pass-through value or Key-value method. However, if the first parameter is passed to the Key-value method, then the following must be the Key-value method, if the first is not, then the subsequent can be passed to the value of the case.
Examples are as follows:
def parrot (voltage= "fff", state= ' a stiff ', action= ' voom ', type= ' Norwegian Blue '): print "--this parrot wouldn ' t", action, print "If you put", voltage, "volts through it." Print "--lovely plumage, the", type print "--It's", State, "!" Parrot (+) #可以 Parrot (action = ' vooooom ', Voltag E = 1000000) #可以, are all Key-value methods Parrot (' A thousand ', state = ' Pushing up the Daisies ') #可以, the first argument is a direct pass-through method, it doesn't matter Parrot (' A million ', ' bereft of life ', ' jump ') #可以, are all values, and since the formal parameters have default values, replace parrot in order (voltage= "", "FF", "ABC") # Can not , the first is the Key-value method, and the future must be
Python beginner, there are four main ways to define a function in Python:
①f (Arg1,arg2,...), the most common way of defining, a function can define any parameter, each parameter is separated by commas, with this parameter must be called in the parentheses after the function name to provide an equal number of values (arguments), and the order must be the same, the form participates in the argument one by one corresponding
def a (x, y): print x, y
Call a function, a (x=1,y=2), if a (1) or a (a) is an error
②f (arg1,arg2=value2,... agrn=valuen), a default value will be provided for the function.
def a (x,y=3): print x, y
Call this function, a (x=1,y=2), if a (1) does not result in an error, the X=1,y=3,y value will use the default value, a (y=4,x=2) the same
Variable parameters:
③f (*ARG1), in the form of a * plus parameter to represent the function of the number of arguments is indeterminate, the number of parameters >=0, in such a way to define the function, in the function of the argument will be the name of the building of a tuple (tuple)
def a (*x): # defines a tuple named X def A (*t): print x >>>a (1) (1,) >>>a () None >>>a (All-in-one) )
One way to traverse the tuple (calculate sum), at which time R is defined as a unary group:
Def y (*r): x = 0 for t in R: x + = t print x
④f (**arg) parameter name plus 2 * * means that inside the function will be stored in the parameter name as an identifier dictionary, the call will use the arg1=value1,arg2=value2 ...
def a (**b): print B >>>a () None >>>a (x=1,y=2) {' Y ': 2, ' X ': 1} #注意遍历返回的顺序与形参位置顺序相反 >>>a ( #error)
The expected key-value pair can be obtained in the following way if the parameter is a key that does not have a ' Y ' defined, and none is returned
def a (**x): print x.get (' y ') >>>a (x=1,y=2) 2 >>>a (x=1) None >>>a (x=1,b=2) None
The python parameter invocation process is reduced in descending order of precedence over the four methods.
① mode parsing, then ② in the Arg=value mode, and then respectively, according to the ③>④ priority to pass the parameter
This is their first through the blog to organize the study notes, hope to myself, to browse to the friends of this has helped, the above function naming does not conform to the specification, only for simple identification instructions, using Python 2.6.2
Hopefully this article will help you with Python programming.