Changing class attributes
You learned how to define class attributes and how to access them from class instances. So what will happen if you try to assign another value to a class attribute when accessing it from an instance? The answer is not as simple as you might think!
The Player
class from the previous exercise is pre-defined. Recall that it has a position
instance attribute, and MAX_SPEED
and MAX_POSITION
class attributes. The initial value of MAX_SPEED
is 3
.
This exercise is part of the course
Object-Oriented Programming in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create Players p1 and p2
____
print("MAX_SPEED of p1 and p2 before assignment:")
# Print p1.MAX_SPEED and p2.MAX_SPEED
____
____
# Assign 7 to p1.MAX_SPEED
____
print("MAX_SPEED of p1 and p2 after assignment:")
# Print p1.MAX_SPEED and p2.MAX_SPEED
____
____
print("MAX_SPEED of Player:")
# Print Player.MAX_SPEED
____