First, define a basic function as an example:
1 def Print (msg): 2 Print (msg)
The function name is print, the parameter has one, for MSG, the function body calls the print system function, Output MSG.
The next step is the variable parameter, which is a special feature, like the params in C #, but different.
Example 1:
1 def Printtuple (*args):2for in args:3 print (ARG) 4 5 printtuple ('Tom','Mary',' Lucy')
Output Tom Mary Lucy in turn.
It is important to note that the args parameter in the Printtuple function is a tuple, that is, a tuple, which is not modifiable. That
1 args[0]='other'
It is worth noting that an exception will occur, unlike the params in C # that encapsulates the parameter as an array.
Example 2:
1 def Printdict (* *args):2for in args:3 print ("key:"", Value:" + str (args[arg])) 4 5 printdict (tom=18,mary=20)
With two asterisks, args becomes a list.
Note, the STR function converts the argument to a string. In this example, the value of the args dictionary is not implicitly converted to string in Integer,python, so the STR function is used to convert.
Python Learning -9.python function definition