how can i create annotations for a list of subclass instances for eg to concatenate two lists?

This situation occurs because list is invariant (provides an illustrative example).

I can offer two solutions:

  1. Explicitly define both lists as List[Animal] for successful concatenation:
cats: List[Animal] = [Cat(height=1, weight=2, lives=7), Cat(height=3, weight=2, lives=1)]
animals: List[Animal] = [Animal(height=9, weight=9)]
combined: Iterable[Animal] = cats + animals

for animal in combined:
  1. Use itertools.chain for consecutive iteration:
cats = [Cat(height=1, weight=2, lives=7), Cat(height=3, weight=2, lives=1)]
animals = [Animal(height=9, weight=9)]

for animal in itertools.chain(cats, animals):

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top