Interfaces define strict API contracts that implementing classes must satisfy.
Use the interface keyword followed by a colon : and indented signatures:
interface FileSystem:
to read(path):
pass
to write(path, data):
pass
Classes declare compliance to interfaces using the with keyword:
class LocalFS with FileSystem:
to read(path):
return fs.read(path)
to write(path, data):
fs.write(path, data)
- Interfaces represent a contract: all declared methods must be implemented by the class.
- Traits allow shared method definitions with default implementations, serving as reusable mixins.