python中的星号(*)和c/c++是不一样的,和指针没有关系,因为python中没有指针的概念。
1.星号(*)被用在函数内部时,星号(*)将一组可变数量的位置参数集合成参数值的元组。
在下面的例子中,输出的值args就是传入到函数print_args的参数值的元组;
建立一个函数:
def print_args(*args):
print(args)
运行:
print_args("dog","cat","bird")
结果:
('dog', 'cat', 'bird')
运行:
print_args()
结果:
()
2.如果函数收集有限的参数,那么args会收集剩下的参数,并保存在一个元组中,比如;
def print_left(first,second,*args): print("this is the first one:",first) print("this is the second one:",second) print("all the rest:",args)
运行:
print_left("dog","cat","bird","mouse","fish")
结果:
this is the first one: dog this is the second one: cat all the rest: ('bird', 'mouse', 'fish')