Inheritance 带参数的Python3类继承

Inheritance 带参数的Python3类继承,inheritance,python-3.x,parameters,attributes,subclass,Inheritance,Python 3.x,Parameters,Attributes,Subclass,我有一个类character()和一个子类npc(character)。它们看起来像这样: class character(): def __init__(self,name,desc): self.name = name self.desc = desc self.attr = "" #large list of attributes not defined by parameters 及 然而,当我从“Char

我有一个类character()和一个子类npc(character)。它们看起来像这样:

class character():
    def __init__(self,name,desc):
        self.name = name
        self.desc = desc
        self.attr = ""    
        #large list of attributes not defined by parameters


然而,当我从“Character”调用一个属性,该属性应该在“Npc”中存在(或者我认为是这样),比如“name”或“desc”或“attr”,我会得到一个“不存在/未定义”错误。我只是做得不对吗?这是怎么回事?我是否混淆了属性和参数?

您的类角色的构造函数是:

class character():
    def __init__(self, name, desc):
所以,当你制作npc herited时,你必须精确地命名和描述。 我个人更喜欢super,这是:

class npc(character):
    def __init__(self,greetings,topics):
        super().__init__("a_name", "a_desc")
        self.greetings = greetings
        self.topics = topics
        self.pockets = []
        #more attributes specific to the npc subclass not defined by parameters

@Metalgearmaycry-不要忘记向上投票并通过单击向上箭头和左侧绿色褪色的勾号接受答案。这让所有人都知道你的问题已经解决:)我知道这个评论已经很晚了,但是有没有办法不使用这些特定参数调用super()。\uuuu init\uuu方法,而是让它们成为我在创建该类的对象时可以指定的变量?类似于:'x=npc(“a_问候语”、“a_主题”、“a_名字”、“a_描述”)?@James您可以在
npc中使用相同的参数。或者使用
**kwargs
并使用关键字unpack传递它们将更加灵活和易于编写。
class npc(character):
    def __init__(self,greetings,topics):
        super().__init__("a_name", "a_desc")
        self.greetings = greetings
        self.topics = topics
        self.pockets = []
        #more attributes specific to the npc subclass not defined by parameters