In this case, we will discuss the parameter passing problem of the function. The python version I use is 3.3.2 for the function: in this case, we want to discuss the parameter passing problem of the function.
The python version I use is 3.3.2.
For functions:
1
2
3
4
5
6
7
8
Def fun (arg ):
Print (arg)
Def main ():
Fun ('Hello, Hongten ')
If _ name _ = '_ main __':
Main ()
When we pass a parameter to the fun () function, we can print the passed parameter value information.
The printed information here is:
Hello, Hongten
For the following use cases:
1
2
3
4
5
6
7
8
9
Def fun (a = 1, B = None, c = None, * args ):
Print ('{0}, {1}, {2}, {3}'. format (a, B, c, args ))
Def main ():
Fun (a = 'one ')
Fun ('one ')
If _ name _ = '_ main __':
Main ()
When the passing parameters are fun (a = 'one') and fun ('one'), the values are copied to parameter, the effects of all two parameters are the same:
One, None, None ,()
One, None, None ,()
Of course, we can also assign values to parameters B, c, and * args.
For example:
1
2
3
4
5
6
7
8
Def fun (a = 1, B = None, c = None, * args ):
Print ('{0}, {1}, {2}, {3}'. format (a, B, c, args ))
Def main ():
Fun ('one', 1, 2, ('hongten '))
If _ name _ = '_ main __':
Main ()
In this way, values are assigned to parameters B, c, and args.
Running effect:
One, 1, 2, ('hongten ',)
In the preceding column, we cannot bypass the parameters a, B, and c in front of * args to copy * args:
For example:
def fun(a=1, b=None, c=None, *args): print('{0},{1},{2},{3}'.format(a, b, c, args)) def main(): fun(args=('hongten')) if __name__ == '__main__': main()
Running effect:
Traceback (most recent call last ):
File "E:/Python33/python_workspace/test_fun.py", line 21, in
Main ()
File "E:/Python33/python_workspace/test_fun.py", line 18, in main
Fun (args = ('hongten '))
TypeError: fun () got an unexpected keyword argument 'args'
However, for parameters a, B, and c, you can assign values in this way.
For example:
def fun(a=1, b=None, c=None, *args): print('{0},{1},{2},{3}'.format(a, b, c, args)) def main(): fun(c=('hongten'), b=2, a=[1,2,3]) if __name__ == '__main__': main()
Running effect:
[1, 2, 3], 2, hongten ,()