def func(*n):
    print(n)

func(1,2,3,4)
func(*(1,2,3))
func((1,2,3,'hey'))
func(('hey',1))


output:
(1, 2, 3, 4)
(1, 2, 3)
((1, 2, 3, 'hey'),)
(('hey', 1),)


将字符串添加到参数时,在元组后出现逗号。

最佳答案

之所以看到,是因为()是一个函数调用,其中(some_object,)是一个元组。

>>> tuple()
()
>>> tuple([None])
(None,)


当您为最后两个函数调用传递args时,请注意双(())

func((1,2,3,'hey'))
func(('hey',1))


因此,您通过的是后两个的元组。查看每种类型

>>> type(('test'))
<class 'str'>
>>> type(('test', 1))
<class 'tuple'>


如果您不希望尾随逗号,请删除参数周围的多余()

10-04 21:57
查看更多