|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class C:
def __init__(self,*x):
self.x = x
def getNumber(self):
if len(x):
print('一共有%d个参数,' % len(x))
for i in x:
print(i)
else:
print('无参数')
>>> c = C()
>>> c = C(1,2,3,4,5,6)
>>> c
标准答案
class C:
def __init__ (self,*args) : # *arg表示不确定个数的参数
if not args :
print ("并没有传入参数")
else :
print ("传入了%d个参数,分别是:"%len(args),end = ' ')
for each in args :
print (each , end = ' ')
c = C()
c = C(1,2,3)
- class C:
- def __init__(self,*x):
- self.x = x
- def getNumber(self): #有报错啊,把这里的x改成self.x
- if len(self.x):
- print('一共有%d个参数:' % len(self.x))
- for i in self.x:
- print(i)
- return "" #避免none(笨办法)
- else:
- # print('无参数') #避免返回值为空显示的“none”
- return "无参数 "
- c1 = C()
- print(c1.getNumber())
- c2 = C(1,2,3,4)
- print(c2.getNumber())
复制代码
|
|