Editor settings

General settings

Save settings

Learn to program with Python

Property specificity #

Property specificity in Python means that if a property with the same name is defined within a class and an object, the value of the object's property takes precedence over the class property.

See the code below for an example:

class Car:
    wheel = 4

regular_car = Car()
different_car = Car()

different_car.wheel = 10

print(regular_car.wheel) # Output: 4
print(different_car.wheel) # Output: 10
print(Car.wheel) # Output: 4

In the example above, when we change the property wheel of the object different_car, only the value in that object is changed to 10, while the same property in both the Car class and regular_car stay the same.

We could even change the Car.wheel property, and different_car.wheel will still be 10.

Car.wheel = 5
print(different_car.wheel) # Output: 10
print(regular_car.wheel) # Output: 5

Exercise #

  • Change the property arms of an object fake_human to be 10.

Tests #

  • Human.arms must be 2
  • regular_human.arms must remain 2
  • fake_human.arms must be 10
Output will be displayed here