Editor

Predict the ABC Error

Intermediate · Predict the output · 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

What does this code print? Think carefully about which abstract methods are implemented and which are missing.

from abc import ABC, abstractmethod

class Database(ABC):
    @abstractmethod
    def connect(self):
        pass

    @abstractmethod
    def disconnect(self):
        pass

class SQLite(Database):
    def connect(self):
        return 'Connected'

try:
    db = SQLite()
except TypeError as e:
    print('Cannot create instance')
# What does the code above print?
# Type your prediction below as a print statement:
print('Cannot create instance')

More Abstract Base Class exercises

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