Oskar Wickström shows off recent Python features (generics + pattern matching) that make writing Python more similar to ML, Haskell, Rust or Scala. If you need to support old versions of Python, you will have to wait a couple years before you use proper generics syntax (although you can use the TypeVar class).
def print_tree[T](tree: RoseTree[T]):
trees = [(tree, 0)]
while trees:
match trees.pop(0):
case Branch(branches), level:
print(" " * level * 2 + "*")
trees = [(branch, level + 1) for branch in branches] + trees
case Leaf(value), level:
print(" " * level * 2 + "- " + repr(value))