Python multiple inheritance from class and protocol

Viewed 538

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!

1 Answers

You can simply add Protocol in the child class:

from typing import Protocol

class SupportsFileOperations(Protocol):
    ...

class SupportsMediaOperations(SupportsFileOperations, Protocol):
    ...
Related