We use Python 3.6

go back Level: Advanced (img: AV / score: 4) level Bite 24. ABC's and class inheritance

21 out of 22 users completed this Bite ...
Will you be Pythonista #22 to crack this Bite?
Average Bite difficulty 1-10 rating: 5.67
» Up for a challenge? 💪

ABC's or Abstract Base Classes are great to enforce a common API for your subclasses.

You define one or more methods and/or properties as abstract in the base class, and if the subclass does not implement them it raises a TypeError. In this bite you will use this concept as follows:

  1. Define a Challenge base class that inherits from ABC (given), its constructor receives a number and a title attribute.
  2. On Challenge define an abstractmethod called verify and an property (< 3.3 it would be an abstractproperty) called pretty_title.
  3. Create the BlogChallenge and BiteChallenge classes which both inherit from Challenge. Note that they would raise a TypeError at this point, exactly what you want: enforcing the use of the abstract method/ property.
  4. BlogChallenge and BiteChallenge's constructors call the parent constructor (don't worry it's supercool, remember: we use Python3 so adjust your syntax), and both receive an extra argument in the constructor: merged_prs for BlogChallenge and result for BiteChallenge.
  5. Implement the required methods and properties, refer to the tests what they need to return.

Get coding, learn more about classes, and have fun!

Github login button