Reputation: 1
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)
Upvotes: 0
Views: 768
Reputation: 939
Assuming you're wondering why the error AttributeError: type object 'BMW' has no attribute 'type'
is being thrown:
You're instantiating an instance of BMW
with: d500 = BMW('manual','500d',2020)
. However, in subsequent lines, you're referring to the class itself rather than the object that you've instantiated.
Since model
, year
and type
are set in the constructor for car
/BMW
, BMW.type
is not defined.
You need to call:
print(d500.type)
print(d500.model)
print(d500.year)
instead in order to refer to your newly created object.
Upvotes: 1
Reputation: 8324
You're trying to print the type
from BMW
, but you just set that object to the variable d500
. Use d500
instead to access the attributes.
d500 = BMW('manual','500d',2020)
print(d500.type)
print(d500.model)
print(d500.year)
Upvotes: 0