1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Object-Oriented Programming in Python

Connected

Exercise

Inheritance of class attributes

Earlier in the course, you learned about class attributes and methods that are shared among all the instances of a class. How do they work with inheritance?

In this exercise, you'll create a subclass of the Player class you worked with earlier in the chapter and explore the inheritance of class attributes and methods.

The Player class has been defined for you, and the code provided here:

class Player:
    MAX_POSITION = 10

    def __init__(self):
      self.position = 0

    def move(self, steps):
      if self.position + steps < Player.MAX_POSITION:
        self.position += steps 
      else:
        self.position = Player.MAX_POSITION                 

Instructions 1/2

undefined XP
    1
    2
  • Create a class Racer, inherited from Player,
  • In the body of the class definition, create a variable called MAX_POSITION and assign a value of 15.
  • Create a Player object p and a Racer object r.