python:*args、**kwargs和assert

 1 """
 2     *args与**kwargs
 3     *args:将参数封装为tuple给函数使用
 4     **kwargs:将参数封装为dic给函数使用
 5 """
 6 
 7 def function1(a, *args):
 8     print(a, args)
 9 function1(1, 2, 3, 4)
10 
11 def function2(a, **kwargs):
12     print(a, kwargs)
13 function2(1, b=2, c=3)
14 
15 def function3(a, b):
16     print(a)
17     print(b)
18 function3(*(1, 2))  # 将元组(1, 2)解析为1, 2
19 function3(**{'a':'hello', 'b':'world'})  # 将字典{'a':'hello', 'b':'word'}解析为a='hello', b='world'
1 (2, 3, 4)
1 {'b': 2, 'c': 3}
1
2
hello
world
1 """
2     assert:不满足条件就抛出异常
3 """
4 assert 1 == 1
5 assert 1 == 2, '1不等于2'
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-22-cb69595573b9> in <module>
      3 """
      4 assert 1 == 1
----> 5 assert 1 == 2, '1不等于2'

AssertionError: 1不等于2

猜你喜欢

转载自www.cnblogs.com/techi/p/11628403.html