Do you know what interfaces are?
#Python doesn't have interfaces as a language feature, but we can replicate what they do using Abstract Base Classes.
But first... why would you want to?

#100DaysOfCode
#Python doesn't have interfaces as a language feature, but we can replicate what they do using Abstract Base Classes.
But first... why would you want to?

#100DaysOfCode
If you're using OOP, a common mistake as codebases grow larger is that you lose track of what methods a class should implement.
For example, you might want all your animal classes to implement a 'num_of_legs' method
For example, you might want all your animal classes to implement a 'num_of_legs' method
You might forget to implement it, or you could misspell the method name.
Using an ABC you can define a class which, when subclassed, forces the subclasses to implement certain methods (otherwise you get an error!).
Here's how you do that
Using an ABC you can define a class which, when subclassed, forces the subclasses to implement certain methods (otherwise you get an error!).
Here's how you do that

Import ABC and abstractmethod from the abc module.
Then, write a class that inherits from ABC.
Define any methods you want to force subclasses to implement as @abstractmethod and don't implement them.
Then, write a class that inherits from ABC.
Define any methods you want to force subclasses to implement as @abstractmethod and don't implement them.
For any class that inherits from Animal, if you don't implement the correct method, you'll get an error when you create an object.
Linters are pretty good at this too, so your editor should help even before running your code!
Linters are pretty good at this too, so your editor should help even before running your code!
Want to learn more about this?
We're writing a blog post to talk about ABCs in more detail.
Follow us here to see when that comes out!
We're writing a blog post to talk about ABCs in more detail.
Follow us here to see when that comes out!