Created
June 24, 2021 18:33
-
-
Save johanvergeer/a0a1d0a915a3f9d67ec6c3b7efbf216c to your computer and use it in GitHub Desktop.
mammals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dog(Mammal): | |
pass | |
class Cat(Mammal): | |
pass | |
TMammal = TypeVar("TMammal", bound=Mammal) | |
class MammalCollection(Generic[TMammal]): | |
def __init__(self, *mammals: TMammal) -> None: | |
self.__mammals = [*mammals] | |
def add(self, mammal: TMammal) -> None: | |
self.__mammals.append(mammal) | |
if __name__ == '__main__': | |
collection = MammalCollection(Dog()) | |
collection.add(Cat()) # This won't work now because it is a mammal collection of Dog() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment