python - Function returns tuple instead of string -
i have function in python similar following:
def checkargs(*args): if len(args) == 1: x = args y = elif len(args) == 2: x, y = args return x, y when put in 1 argument (a string), x comes out tuple. when put in 2 arguments (two strings), x , y returned strings. how can x come out string if put in 1 argument?
this happens because args always tuple, if put in 1 argument. so, when do:
x = args this doing:
x = ('abc',) there 2 (equivalent) ways fix this: either explicitly assign x first element of tuple:
x = args[0] or invoke same tuple unpacking x,y assignment uses assigning length-1 tuple:
x, = args
Comments
Post a Comment