Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using Abstract Base Classes

00:00 Using Abstract Base Classes or ABCs. Abstract base classes, or ABCs, define a specific set of public methods and attributes, or API, that all their subclasses must implement.

00:14 They set rules for subclasses, so they make sure that every subclass has the same required methods. They catch mistakes early, so if a class is missing a required method, Python will warn you before you run the code.

00:28 And they use Python’s abc module. The abc module lets you define ABCs and enforce structure in your classes.

00:38 Now let’s explore this example. Say that you want to create classes to represent different vehicles, and you want the classes to have the following methods: .start(), .stop(), and .drive().

00:52 Here’s your base code. You have two classes, Car and Truck. Both classes have an .__init__() method, which is a constructor.

01:00 When you create instances of Car or Truck, you need to provide three attributes: make, model, and color.

01:08 These get stored inside the object. Then both classes define three methods: .start(), .drive(), and .stop(). These just print messages when called.

01:18 For example, the car is starting or the truck is starting. So if you create a car object and call .start(), it will print the car is starting, and if you do the same with a truck, it will say the truck is starting.

01:31 These classes share the expected interface. They’re independent of each other and decoupled. They don’t need each other to work correctly, so you can use them interchangeably in a duck typing context, like this one.

01:44 First, you’re importing Car and Truck from vehicles_duck, which means you’re using the code from before.

01:50 Next, you create a list called vehicles that holds two objects, a car and a truck. The car is a red Ford Mustang, and the truck is a blue Ford F-150.

02:01 So now you’ve got two different vehicle objects, but they both share the same behavior. Then, you’re looping through the vehicles list and you’re calling three methods on each one: .start(), .drive(), and .stop().

02:14 Since both Car and Truck have these methods, Python just runs them without worrying about whether the object is a car or a truck.

02:22 And as you know, this is duck typing in action. When you run the code, Python goes through the list calling each method on the car first, and then on the truck.

02:31 That’s why you see the output. The car starts, drives, and stops, and then the truck starts, drives, and stops.

02:39 As you might have noticed, you’re not seeing any errors. This works fine because both classes happen to have the same methods. But the problem with duck typing here is that nothing guarantees all vehicle classes will have these methods.

02:53 Say if you wanted to add a Jeep class but forgot to include the stop() method, the code will crash at runtime with an AttributeError.

03:03 Since Python doesn’t check for missing methods ahead of time, you might not catch the mistake until it actually breaks. And honestly, that’s fine in a small script, but in a bigger project, it can be a headache.

03:15 Now with an abstract base class or ABC, you can make sure every vehicle class has the required methods before the code even runs.

03:24 How? Let’s find out. Let’s create an ABC called Vehicle that defines the required interface and makes all your vehicle classes inherit from it.

Become a Member to join the conversation.