Transfer articles (1)
When a parameter is *arg, it represents an incoming array, and the incoming dictionary is represented when the formal parameter is **args. Python Code def myprint (*COMMENDS,**MAP): For comm in Commends:print Comm for key in Map.keys (): Print K Ey,map[key] Myprint ("Hello", "word", username= "Tian", Name= "Wei")
Output:
Hello
Word
Username Tian
Name Wei
Python defines a function that can be called by a normal method that is passed in only a value or Key-value. However, if the first parameter is passed in the Key-value method, then the following must be the Key-value method, if the first is not, then the following can be passed to the value of the next.
Example: Python code 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 (1000) #可以 Parrot (action = ' vooooom ', voltage = 1000000) #可以, are Key-value method Parrot (' A thousand ', state = ' pushing Up the Daisies ') #可以, the first argument is the direct pass, followed by the parrot (' A million ', ' bereft of life ', ' jump ') #可以, are all values, and because the formal parameters have default values, they are replaced in order Parrot (voltage= "", "FF", "ABC") # No, the first one for Key-value, the next must be.
Transfer articles (2)
Getting Started with Python beginners, there are four main ways to define functions in Python:
[1]
F (Arg1,arg2,...), the most common way of defining, a function can define any parameter, separated by commas, with which the number of equal values (arguments) must be supplied in parentheses following the function name, and the order must be the same, and the form participates in the argument one by one corresponding
Python Code def A (x,y): Print X,y
def a (x,y):
print X,y
Call a function, a (1,2) x=1,y=2, if a (1) or a (1,2,3) error
[2]
F (arg1,arg2=value2,... agrn=valuen), the default value is provided for the function. Python Code def A (x,y=3): Print X,y
def a (x,y=3):
print X,y
Call the function, a (1,2) x=1,y=2, if a (1) does not cause an error, the X=1,y=3,y value will use the default value, a (y=4,x=2)
Variable parameters
[3]
F (*ARG1), which represents an indeterminate number of arguments for a function by means of a *-plus parameter, the number of parameters is >=0, the function is defined in such a way that a tuple (tuple) Python code def A (*x) is constructed within the function by the argument name: # defines a tuple named X
def a (*x):
# defines a tuple named X
Python Code def A (*t): Print X
def a (*t):
print X
>>>a (1)
(1,)
>>>a ()
None
>>>a (1,2,3)
(1,2,3)
One way to traverse the tuple (calculate the sum), where r is defined as a unary group: Python code def y (*r): x = 0 for T in r:x + = t print X
Def y (*r):
x = 0 for
t in R:
x + = t
print x
[4]
An F (**arg) parameter name plus 2 * * means that it will be stored inside the function in a dictionary with the parameter name identifier, when the call will use arg1=value1,arg2=value2 ... Python Code def A (**b): Print B
def a (**b):
print B
>>>a ()
None
>>>a (x=1,y=2)
{' Y ': 2, ' X ': 1} #注意遍历返回的顺序与形参位置顺序相反
>>>a (1,2) #error
You can obtain an expected key value pair in the following ways, if the parameter is a key that does not define ' Y ', returns none Python code def A (**x): Print x.get (' y ')
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 procedure is lowered in order of precedence for the above four methods.
[1] The way is resolved, then the Arg=value in [2], followed by [3]>[4] Precedence
This is my first blog to organize the study notes, I hope to myself, to browse to the friends to help, the above function name does not conform to the specification, only for simple identification instructions, using Python 2.6.2