Python function parameters are commonly used in computer languages, but they are difficult to run. For example, the Python function parameters and the command line parameters in ython are passed through the value assignment. The following is an introduction to it. I hope you will gain some benefits.
The usage of function parameters is worth noting in two aspects:
- >>> def printpa(**a):
- ... print type(a)
- ... print a
- ...
- >>> printpa(a=1,y=2)
- <type 'dict'>
- F(arg1,arg2,...)
- {'a': 1, 'y': 2}
- >>> printpa(a=1)
- <type 'dict'>
- {'a': 1}
- >>> li=[1,2,3,4]
- >>> printpa(b=li)
- <type 'dict'>
- {'b': [1, 2, 3, 4]}
- >>> tu=(1,2,3)
- >>> printpa(b=tu)
- <type 'dict'>
- {'b': (1, 2, 3)}
- >>> printpa(1,2)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: printpa() takes exactly 0 arguments (2 given)
F (arg1, arg2 = value2 ,...)
Is the most common definition method. A function can define any parameter. Each parameter is separated by a comma, when calling a function defined in this way, the actual number of values must be provided in the parentheses after the function name), and the order must be the same, that is, in this call method, the number of parameters must be the same and one-to-one, that is, the first parameter corresponds to the first real parameter. For example:
- def a(x,y):
- print x,y
Call this Python function parameter. If a () is used, x takes 1, y takes 2, and the form corresponds to the real parameter. If a (1) or a (, 3) an error is reported. Let's look at the following example:
- >>> a=(1,2,3)
- >>> def printpa(a):
- ... print type(a)
- ... print a
- ...
- >>> printpa(a)
- <type 'tuple'>
- (1, 2, 3)
- >>> printpa(range(1,4))
- <type 'list'>
- [1, 2, 3]
- >>> printpa({})
- <type 'dict'>
- {}
- >>> def printpa(a,b,c):
- ... print a,b,c
- ...
- >>> printpa(a)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: printpa() takes exactly 3 arguments (1 given)
- >>> printpa(*a)
- 1 2 3
- >>> a=[1,2,3]
- >>> printpa(*a)
- 1 2 3
- >>> printpa(a)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: printpa() takes exactly 3 arguments (1 given)
- >>> a=[1,2,3,4]
- >>> printpa(*a)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: printpa() takes exactly 3 arguments (4 given)
- >>> printpa(*range(1,4))
- 1 2 3
It can be seen from the above that if a function has multiple form parameters, a single tuples or list can be passed for real parameters during the call, however, the number of elements in the tuples or list must be the same as that of the form parameter. The above article is about Python function parameters and command line parameters. The transfer of function parameters in ython is the basic application introduction of passing values.
- Comparison of Python function and Ruby related technologies
- Technical Comparison Between the Python programming language and Java
- Five methods for Python string operations
- Detailed introduction to python multi-thread applications
- How to Use Python script code in C ++