Junior — Middle
Do you include specific interfaces in your code structure?
sobes.tech AI
Answer from AI
In Python, there is no strict need to use interfaces like in Java or C#, since the language is dynamic and supports duck typing. However, to improve code structure and explicitly specify contracts, you can use:
- Abstract Base Classes (ABC) from the
abcmodule. They allow you to define methods that must be implemented in subclasses.
Example:
from abc import ABC, abstractmethod
class MyInterface(ABC):
@abstractmethod
def do_something(self):
pass
class Implementation(MyInterface):
def do_something(self):
print("Performing an action")
Using ABC helps make the code more readable and maintainable, especially in large projects.