Skip to content

Instantly share code, notes, and snippets.

@johanvergeer
Created June 24, 2021 18:33
Show Gist options
  • Save johanvergeer/a0a1d0a915a3f9d67ec6c3b7efbf216c to your computer and use it in GitHub Desktop.
Save johanvergeer/a0a1d0a915a3f9d67ec6c3b7efbf216c to your computer and use it in GitHub Desktop.

Revisions

  1. johanvergeer created this gist Jun 24, 2021.
    19 changes: 19 additions & 0 deletions mammals.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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()