刷题(七)
题目
请写出下列代码的运行结果
def f(str1, *args, **kwargs): print(str1, args, kwargs) l = [1, 2, 3] t = [4, 5, 6] d = {"a": 7, "b": 8, "c": 9} f(1, 2) #1 f(1, 2, 3, "python") #2 f("python", a=1, b=2, c=3) #3 f("python", l, d) #4 f("python", *t) #5 f("python", *l, **d) #6 f("python", q="winning", **d) #7
思路
题目不难,但很容易漏,就像在#1
中,实际上给关键字参数**kwargs什么都没传,打印时,仍然会打印个空字典{}
,同样在#3
中,可变参数*args位置上什么都没传,仍然会打印个空元组()
值得注意的还有#4
,l
和d
,由于都没有解包,所以当做一个元组([1, 2, 3], {"a": 7, "b": 8, "c": 9})
传进去了
结果
1 (2,) {} 1 (2, 3, ‘python‘) {} python () {‘a‘: 1, ‘b‘: 2, ‘c‘: 3} python ([1, 2, 3], {‘a‘: 7, ‘b‘: 8, ‘c‘: 9}) {} python (4, 5, 6) {} python (1, 2, 3) {‘a‘: 7, ‘b‘: 8, ‘c‘: 9} python () {‘q‘: ‘winning‘, ‘a‘: 7, ‘b‘: 8, ‘c‘: 9}
相关推荐
huavhuahua 2020-11-20
weiiron 2020-11-16
cakecc00 2020-11-15
千锋 2020-11-15
JakobHu 2020-11-14
guangcheng 2020-11-13
xirongxudlut 2020-11-10
solarLan 2020-11-09
pythonxuexi 2020-11-08
文山羊 2020-11-07
susmote 2020-11-07
wuShiJingZuo 2020-11-05
Pythonjeff远 2020-11-06
jacktangj 2020-11-04
lousir 2020-11-04
Noneyes 2020-11-10
ailxxiaoli 2020-11-16
chensen 2020-11-14
Nostalgiachild 2020-11-13