In Python, I have two protocols, one of which inherits from the other:
class SupportsFileOperations(Protocol):
...
class SupportsMediaOperations(SupportsFileOperations):
...
I then have a couple of concrete classes that implement these protocols, and one inherits from the other.
class File(SupportsFileOperations):
...
class MediaFile(File, SupportsMediaOperations):
def __init__(self):
File.__init__(self)
My question is, is calling File.__init__(self) in the MediaFile constructor the correct way to initialize this? I'm not sure how multiple inheritance works with protocols.
Thanks!
You can simply add Protocol in the child class:
from typing import Protocol
class SupportsFileOperations(Protocol):
...
class SupportsMediaOperations(SupportsFileOperations, Protocol):
...
When you using protocols in Python you use structural subtyping, so you don't have to inherit the protocol classes. If you want some class to be considered as a subtype of your protocol you just need to implement all methods of protocol with the same function signatures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With