函数中可变数量的参数
Variable number of arguments in function
是否可以运行这样的函数:
def call(a,*alphabets,*numbers):
print(a)
print(alphabets)
print(numbers)
我收到以下错误:
File "<ipython-input-331-ddaef8a7e66f>", line 1
def call(a,*alphabets,*numbers):
^
SyntaxError: invalid syntax
有人可以告诉我是否有替代方法吗?
很简单:要求调用者传递两个列表(或元组或其他):
def call(a,alphabets=None,numbers=None):
if alphabets is None:
alphabets = []
if numbers is None:
numbers = []
print(a)
print(alphabets)
print(numbers)
call("?")
call("?", ["a", "b", "c"])
call("?", ["a", "b", "c"], (1, 2, 3))
call("?"), None, (1, 2, 3))
# etc
是否可以运行这样的函数:
def call(a,*alphabets,*numbers):
print(a)
print(alphabets)
print(numbers)
我收到以下错误:
File "<ipython-input-331-ddaef8a7e66f>", line 1
def call(a,*alphabets,*numbers):
^
SyntaxError: invalid syntax
有人可以告诉我是否有替代方法吗?
很简单:要求调用者传递两个列表(或元组或其他):
def call(a,alphabets=None,numbers=None):
if alphabets is None:
alphabets = []
if numbers is None:
numbers = []
print(a)
print(alphabets)
print(numbers)
call("?")
call("?", ["a", "b", "c"])
call("?", ["a", "b", "c"], (1, 2, 3))
call("?"), None, (1, 2, 3))
# etc