编写函数来传递参数或参数元组/列表的最 Pythonic 方法是什么?
例如,函数可以接受或add的参数,并同时输出。add(1, 2)add((1, 2))3
到目前为止我所拥有的:(它有效,但看起来不太好)
def add(*args):
if len(args) == 1:
return (args[0][0] + args[0][1])
if len(args) == 2:
return args[0] + args[1]
else:
print "error: add takes in one or two arguments"
Run Code Online (Sandbox Code Playgroud)
我不喜欢它的是:
args[0][0]很难读