Question about Object syntax

I´m practicing creating objects and I’ve run into a snafu in relation to a syntax error that I don’t quite understand. VSCode tells me this:

AttributeError: type object 'Vehicle' has no attribute 'name'

Yet, I have created it. I am attempting to make the process of inheritance smoother by referencing each object to the class of Vehicle I’ve created, but Python doesn’t recognize this. What am I missing?
The code is below.

class Vehicle:

    def __init__(self, name, max_speed, mileage, color):
        self.name = name
        self.max_speed = max_speed
        self.mileage = mileage
        self.color = color
Vehicle.color = "white"
print("The ", {Vehicle.name}, " is ", {Vehicle.color}, " running ", {Vehicle.max_speed}, " and with a mileage of ", {Vehicle.mileage})

class Bus(Vehicle):
    pass

class Car(Vehicle):
    pass


Bus("School Volvo", 180, 12)
Car("Audi Q5", 240, 18)

print(Bus(Vehicle))
print(Car(Vehicle))

name and other attributes assigned to the self.x refer to the specific object instance. They are assigned when creating concrete object.

>>> Vehicle.name
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    Vehicle.name
AttributeError: type object 'Vehicle' has no attribute 'name'

>>> v = Vehicle('car', 160, 20, 'yellow')
>>> v.name
'car'

>>> Vehicle.name
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    Vehicle.name
AttributeError: type object 'Vehicle' has no attribute 'name'

The problem is You Initialized Vehicle.color = White
But you are printing Vehicle.name and Vehicle.mileage without initializing them.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.