跟老齐学Python之变量和参数

2019-10-05 15:50:37于丽

{'a': 1, 'c': 3, 'b': 2}

 如果这次还用foo(1,2,3)的方式,会有什么结果呢?

>>> foo(1,2,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 0 arguments (3 given)

 看官到这里可能想了,不是不确定性吗?我也不知道参数到底会可能用什么样的方式传值呀,这好办,把上面的都综合起来。

>>> def foo(x,y,z,*args,**kargs):
...     print x  
...     print y
...     print z
...     print args
...     print kargs       
...
>>> foo('qiwsir',2,"python")
qiwsir
2
python
()
{}
>>> foo(1,2,3,4,5)
1
2
3
(4, 5)
{}
>>> foo(1,2,3,4,5,name="qiwsir")
1
2
3
(4, 5)
{'name': 'qiwsir'}

 很good了,这样就能够足以应付各种各样的参数要求了。