| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
- >>> class Ball:
 
 -         def setname(name):
 
 -                 self.name = name
 
 -                 print(self.name)
 
  
-                 
 
 - >>> a = Ball()
 
 - >>> b = Ball()
 
 - >>> a.setname('a')
 
 - Traceback (most recent call last):
 
 -   File "<pyshell#36>", line 1, in <module>
 
 -     a.setname('a')
 
 - TypeError: setname() takes 1 positional argument but 2 were given
 
 - >>> b.setname('b')
 
 - Traceback (most recent call last):
 
 -   File "<pyshell#37>", line 1, in <module>
 
 -     b.setname('b')
 
 - TypeError: setname() takes 1 positional argument but 2 were given
 
  复制代码 
为什么缺少self参数后会报这样的错误?
任何一个类对象调用函数的时候都默认有一个self参数 
你写a.setname('a')除了‘a',还有self 
但是你的函数定义中没有self,所以报错  
 
 
 |   
 
 
 
 |