This is really odd - I have 11 labels being passed into LabelTree. The first time I call sort_by_hierarchy, everything is fine.
I refresh the page, an AJAX call gets shot off to call this function, and then it returns 22 labels. I refresh the page again, 44 labels.
What am I doing wrong?
class LabelTree:
    def __init__(self, labels):
        self.by_parent = {}
        self.by_id = {}
        for label in labels:
            parent_id = 0 if label.parent_id is None else label.parent_id
            if parent_id not in self.by_parent:
                self.by_parent[parent_id] = []
            self.by_parent[parent_id].append(label)
            self.by_id[label.id] = label
    def sort_by_hierarchy(self, parent_id=0, depth=0, hierarchical_labels=[]):
        if parent_id not in self.by_parent:
            return hierarchical_labels
        labels = self.by_parent[parent_id]
        for label in labels:
            label.depth = depth
            hierarchical_labels.append(label)
            hierarchical_labels = self.sort_by_hierarchy(label.id, depth+4, hierarchical_labels)
        return hierarchical_labels