Suppose I have a python function that takes two arguments, but I want the second arg to be optional, with the default being whatever was passed as the first argument. So, I want to do something like this:
def myfunc(arg1, arg2=arg1):
print (arg1, arg2)
Except that doesn't work. The only workaround I can think of is this:
def myfunc(arg1, arg2=None):
if arg2 is None:
arg2 = arg1
print (arg1, arg2)
Is there a better way to do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…