- 参数的顺序是位置参数、*arg、**kwarg。
- **kwargs只能通过x=1,y=2这种方式传入,函数里面只能用kwargs或者*kwargs。
- *arg对应得形参数量有贪婪属性。
def function(x, *arg, **kwargs):
print(x, "|\t", *arg, "|\t", **kwargs)
function(1, [1, 2, 3], {"z": 1, "y": 2})
function(1, 1, 2, 3, {"z": 1, "y": 2})
print('\n')
def function(x, *arg, **kwargs):
print(x, "|\t", arg, "|\t", **kwargs)
function(1, [1, 2, 3], {"z": 1, "y": 2})
function(1, 1, 2, 3, {"z": 1, "y": 2})
print('\n')
def function(x, *arg, **kwargs):
print(x, "|\t", arg, "|\t", *arg, "|\t", *kwargs)
function(1, 1, 2, 3, z=1, y=2)
print('\n')
def function(x, *arg, **kwargs):
print(x, "|\t", arg, "|\t", *arg, "|\t", kwargs, "|\t", *kwargs)
function(1, 1, 2, 3, z=1, y=2)
print('\n')
E:\pythonPrj\common\venv\Scripts\python.exe E:/pythonPrj/common/arg_kwarg.py
1 | [1, 2, 3] {'z': 1, 'y': 2} |
1 | 1 2 3 {'z': 1, 'y': 2} |
1 | ([1, 2, 3], {'z': 1, 'y': 2}) |
1 | (1, 2, 3, {'z': 1, 'y': 2}) |
1 | (1, 2, 3) | 1 2 3 | z y
1 | (1, 2, 3) | 1 2 3 | {'z': 1, 'y': 2} | z y
Process finished with exit code 0