当我在python中使用类时,代码可以工作,但linter用红色标记显示代码中的错误

当我在python中使用类时,代码可以工作,但linter用红色标记显示代码中的错误,python,python-3.x,oop,Python,Python 3.x,Oop,我是oop新手 所以,当我想在子类中有变量/统计信息,在父类中有它们的方法,这样我就不必重复了,即使代码可以工作,代码中也有一条蜿蜒的线 我做错什么了吗 class User(): level_xp = { 1: 250, 2: 900, 3: 4500, 4: 9000, 5: 45000 } def exp_after_battle(self): self.xp += 2

我是oop新手 所以,当我想在子类中有变量/统计信息,在父类中有它们的方法,这样我就不必重复了,即使代码可以工作,代码中也有一条蜿蜒的线 我做错什么了吗

class User():
    level_xp = {
        1: 250,
        2: 900,
        3: 4500,
        4: 9000,
        5: 45000
    }

    def exp_after_battle(self):
        self.xp += 2400 * self.base_multiplier

        if self.xp > User.level_xp[self.level]:
            self.xp -= User.level_xp[self.level]
            self.level += 1

    def login(self):
        return 'signed in'


class Witch(User):
    def __init__(self, name, subs):
        self.name = name
        self.xp = 0
        self.level = 1
        self.base_multiplier = subs

    pass


testsubject = Witch('testsubject', 1)
testsubject.exp_after_battle()
testsubject.exp_after_battle()
testsubject.exp_after_battle()
testsubject.exp_after_battle()
testsubject.exp_after_battle()
testsubject.exp_after_battle()

print(testsubject.level)
在“问题”选项卡中显示以下错误

Unresolved attribute reference 'xp' for class 'User':11
Unresolved attribute reference 'base_multiplier' for class 'User':11
Unresolved attribute reference 'xp' for class 'User':13
Unresolved attribute reference 'level' for class 'User':13
Unresolved attribute reference 'xp' for class 'User':14
Unresolved attribute reference 'level' for class 'User':14
Unresolved attribute reference 'level' for class 'User':15

代码是有效的,我不知道linters为什么需要用
\uuuu init()\uuuu
方法实例化用户类,以允许它拥有自己的变量

class User():
定义初始化(自):
self.level_xp={
1: 250,
2: 900,
3: 4500,
4: 9000,
5: 45000
}
#代码的其余部分。。。

在父类中有共享方法是个好主意。但是,这也意味着您在此类方法中使用的所有变量也应该在父类中定义,即在其
\uuuu init\uuuu
函数中定义。否则,您可能会引入一个新的
Orc
类,但忘记初始化它的xp。然后,
exp\u-after\u-battle
将引用一个未定义的变量

正如您意识到
exp\u after\u battle
属于父类一样,您必须针对所有属性问自己同样的问题。是否所有用户都有名称、级别、xp等?其中哪些具有子类不应指定的合理默认值?我认为为所有用户设置默认值
self.xp=0
self.level=1
self.base\u乘数=1
似乎是明智的。此外,所有用户都有一个名称,但没有要设置的默认值,因此我们应该将其作为
\uuuuu init\uuuu
方法的参数。然后我们可以调用子类中的
super
函数来设置自定义名称以及所有默认属性。之后,我们还可以设置自定义的基本乘数

这将导致以下代码:

类用户:
级别_xp={
1: 250,
2: 900,
3: 4500,
4: 9000,
5: 45000
}
定义初始化(self,name):
self.name=名称
self.xp=0
self.level=1
self.base_乘数=1
战斗后def exp_(自我):
self.xp+=2400*self.base\u乘数
如果self.xp>User.level\u xp[self.level]:
self.xp-=User.level\u xp[self.level]
self.level+=1
def登录(自我):
返回“已登录”
类别(用户):
定义初始(自身、名称、基本乘数):
super()。\uuuu init\uuuu(名称)
self.base\u乘数=base\u乘数
testsubject=Witch('testsubject',1)
testsubject.exp_-after_-battle()
testsubject.exp_-after_-battle()
testsubject.exp_-after_-battle()
testsubject.exp_-after_-battle()
testsubject.exp_-after_-battle()
testsubject.exp_-after_-battle()
打印(testsubject.level)

但问题在于,与其他heros类相比,witch将拥有自己的统计数据,因此在它们的类中,heros的方法和统计数据不可能有一个位置吗?您是指自定义变量还是自定义值(例如,不同的基乘数)?因为您不能在基类中使用自定义变量,而是必须覆盖或引入子类中的方法。比如说,女巫每次战斗自动获得100额外经验。然后您必须在子类
Witch
中覆盖
exp\u after\u battle