Skip to content

Instantly share code, notes, and snippets.

@johanvergeer
Created June 24, 2021 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johanvergeer/a0a1d0a915a3f9d67ec6c3b7efbf216c to your computer and use it in GitHub Desktop.
Save johanvergeer/a0a1d0a915a3f9d67ec6c3b7efbf216c to your computer and use it in GitHub Desktop.
mammals
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