在function中,我定义了两个参数1:default变量age=12和2:variable-length参数*friends
def variable(age=12,*friends):
print 'Age:', age
print 'Name:', friends
return
variable(15,'amit','rishabh') # RESULT is "Age: 15 & Name: 'amit', 'rishabh'
variable('rahul','sourabh') # Now here result is Age: rahul & Name: 'sourabh' 所以我的问题是,为什么函数没有在*friends变量中同时接受这两个参数,为什么它将第一个参数确定为年龄。
我需要结果应该是这样的格式:
variable(15,'name','surname') as Age:15 and Name: 'name','surname'如果我不把年龄指定为
variable('new','name') Result needed to be as. Age:12 & Name:'new','name'发布于 2018-09-10 15:19:50
你可以尝试给出一个列表,而不是各种参数,而且关键字参数应该总是在aguments之后:
def variable(friends, age=12):
print 'Age:', age
print 'Name:', ",".join(friends)
return
variable(['amit','rishabh'], 15) # RESULT is "Age: 15 & Name: 'amit', 'rishabh'
variable(['rahul','sourabh']) # Now here result is Age: rahul & Name: 'sourabh'发布于 2018-09-10 15:17:57
尝试切换参数:
def variable(*friends,age=12):
print ('Age:', age)
print ('Name:', friends)
return这应该是可行的。
https://stackoverflow.com/questions/52252586
复制相似问题