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
armsof an objectfake_humanto be10.
Tests #
Human.armsmust be2regular_human.armsmust remain2fake_human.armsmust be10
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules