Editor

Fix the Broken ABC

Intermediate · Fix the bug · from Python Abstract Base Classes: Enforce Interfaces with ABC

From the lesson: Python Abstract Base Classes: Enforce Interfaces with ABC

Imagine you're an architect handing blueprints to a construction crew. The blueprint says "there must be a front door here" and "the roof goes here." The crew can choose the materials and style, but they must include those parts. If they skip the front door, the building inspector will reject the project.

Read the lesson

This code is supposed to define an abstract Logger class and a FileLogger that implements it. But it has bugs! Fix them so the output is:

Logging to file: Server started
from abc import ABC, abstractmethod
class Logger():
@abstractmethod
def log(self, message):
pass
class FileLogger(Logger):
def write(self, message):
return f'Logging to file: {message}'
logger = FileLogger()
print(logger.log('Server started'))

More Abstract Base Class exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz