xu502108 2021-12-12 14:09 采纳率: 0%
浏览 299

怎么解决 NameError: name 'self' is not defined? 求解答

问题相关代码,请勿粘贴截图

```python
class Ball():
def init(self,color,size,type):
self.color = color
self.size = size
self.type = type
def Play(self):
print('踢')
ball=Ball('白色','小','足球')
print(ball.color+ball.size+ball.type)
ball.Play(self)

```###### 运行结果及报错内容
白色小足球
Traceback (most recent call last):
ball.Play(self)
NameError: name 'self' is not defined

我想要达到的结果

踢白色小足球

  • 写回答

1条回答 默认 最新

  • bekote 2021-12-12 14:51
    关注
    
    class Ball():
        def __init__(self,color,size,type):
            self.color = color
            self.size = size
            self.type = type
        def Play(self):
            print('踢')
    ball=Ball('白色','小','足球')
    print(ball.color+ball.size+ball.type)
    ball.Play()
    
    评论

报告相同问题?

问题事件

  • 修改了问题 12月12日
  • 创建了问题 12月12日