Python AttributeError:type object';宝马&x27;没有属性';类型';

Python AttributeError:type object';宝马&x27;没有属性';类型';,python,inheritance,attributeerror,Python,Inheritance,Attributeerror,您正试图从BMW打印类型,但您只是将该对象设置为变量d500。使用d500访问属性 class car: def __init__(self,model,year): self.model = model self.year = year class BMW(car): def __init__(self,type,model,year): car.__init__(self,model,year)

您正试图从
BMW
打印
类型
,但您只是将该对象设置为变量
d500
。使用
d500
访问属性

class car:
        def __init__(self,model,year):
            self.model = model
            self.year = year


class BMW(car):
    def __init__(self,type,model,year):
        car.__init__(self,model,year)
        self.type = type

class Audi(car):
    def __init__(self,type1,model,year):
        car.__init__(self, model, year)
        self.type1 = type1

d500 = BMW('manual','500d',2020)
print(BMW.type)
print(BMW.model)
print(BMW.year)

您在这里并没有真正提出问题,但您可能想知道为什么会抛出错误
AttributeError:type对象“BMW”没有属性“type”

您正在实例化
BMW
的一个实例:
d500=BMW('manual','500d',2020)
。但是,在后面的几行中,您引用的是类本身,而不是实例化的对象

由于
车型
年份
类型
汽车
/
宝马
的构造函数中设置,因此未定义
宝马.类型

您需要拨打:

d500 = BMW('manual','500d',2020)
print(d500.type)
print(d500.model)
print(d500.year)

而是为了引用新创建的对象。

您必须调用对象的实例化,而不是类名。在本例中,您将对象命名为
d500
,因此您希望调用
d500.type
d500.model
d500.year
为什么要打印
BMW.type
等。?您应该打印
d500。键入
之类的内容。如果您这样做,您的代码工作良好(经过测试)非常感谢!!!事实上,当我看到那个少年犯的错误时,我嘲笑自己。非常感谢你的解释,兄弟。这真的意味着无需担心,很高兴我能帮上忙。是的,兄弟,我犯了一个愚蠢的错误,但无论如何,非常感谢你抽出时间来解释和回答我的问题。。
print(d500.type)
print(d500.model)
print(d500.year)