Introduction to parameter passing and variable length parameters of functions in Python;
1. There are also default functions like C ++ in Python.
Copy codeThe Code is as follows:
Def foo (text, num = 0 ):
Print text, num
Foo ("asd") # asd 0
Foo ("def", 100) # def 100
When defining a function with default parameters, these default parameters must be placed behind non-default parameters.
When the default value is provided, the provided value is used. Otherwise, the default value is used.
2. Python can pass parameters according to the parameter name
Copy codeThe Code is as follows:
Def foo (ip, port ):
Print "% s: % d" % (ip, port)
Foo ("192.168.1.0", 3306) #192.168.1.0: 3306
Foo (port = 8080, ip = "127.0.0.1") #127.0.0.1: 8080
Row 3, with no parameter name specified, transmits parameters in sequence.
Line 2: Specify the parameter name. You can pass the parameter by parameter name.
3. Variable Length Parameters
Copy codeThe Code is as follows:
# Coding: UTF-8 # Set the python file encoding to UTF-8, so that you can write Chinese comments.
Def foo (arg1, * tupleArg, ** dictArg ):
Print "arg1 =", arg1 # formal_args
Print "tupleArg =", tupleArg #()
Print "dictArg =", dictArg # []
Foo ("formal_args ")
The parameter in the above function, "*" before tupleArg indicates that this parameter is a tuples parameter. From the output of the program, we can see that the default value is (); "**" in front of dicrtArg indicates this dictionary parameter (key-Value Pair parameter ). TupleArg and dictArg can be considered as two default parameters. Redundant non-Keyword parameters are placed in the tupleArg parameter of the tuples during function calls. Redundant keyword parameters are placed in the dictionary parameter dictArg.
The following are some usage of variable length parameters:
Copy codeThe Code is as follows:
# Coding: UTF-8 # Set the python file encoding to UTF-8, so that you can write Chinese comments.
Def foo (arg1, arg2 = "OK", * tupleArg, ** dictArg ):
Print "arg1 =", arg1
Print "arg2 =", arg2
For I, element in enumerate (tupleArg ):
Print "tupleArg % d --> % s" % (I, str (element ))
For key in dictArg:
Print "dictArg % s --> % s" % (key, dictArg [key])
MyList = ["my1", "my2"]
MyDict = {"name": "Tom", "age": 22}
Foo ("formal_args", arg2 = "argSecond", a = 1)
Print "*" * 40
Fool (123, myList, myDict)
Print "*" * 40
Foo (123, rt = 123, * myList, ** myDict)
Output:
From the above procedure, we can see that:
(1) line 3 of the Code.
If "*" tuples or "**" dictionary parameters are used in parameters, these two parameters should be placed at the end of the parameter list. The "*" parameter is located before the "**" dictionary parameter.
Keyword parameter rt = 123, because the function foo (arg1, arg2 = "OK", * tupleArg, ** dictArg) does not have the rt parameter, it is also included in the dictionary parameter.
(2) line 3 of the Code.
If "*" is not included in the front of the object, or if "*" is not included in the front of the dictionary object, it is used as a common object to pass parameters.
For redundant common parameters, in foo (123, myList, myDict), 123 is assigned to arg1 and myList to arg2. The redundant parameter myDict is assigned to myList by default.