Suppose I have these two classes:
class Branch():
def __init__(self, id, leaves):
self.id = id
self.leaves = leaves
class Leaf():
def __init__(self, color)
self.color = color
def describe()
print("This leaf is " + color)
Every Leaf belongs to a Branch. A Branch continues to exist whether it has any leaves or not, but a Leaf cannot meaningfully exist without a Branch (real-life logic aside). That means I can easily get every Leaf from a Branch through the Branch.leaves attribute. If I understood composition correctly, a Branch is the composite and every Leaf in Branch.leaves is one of its components?
However, no Leaf actually knows to which Branch it belongs to. So, I can't have it print "This leaf is orange and belongs to branch #14". I could just add a Leaf.branch attribute and pass it whenever I initialize a new Leaf instance, seeing how a Branch must already exist for that to happen, but that seems very wrong to me, because then it looks like the Branch belongs to the Leaf.
The question is: what is the most pythonic and correct way to do what I described I can't do in my example? Is what I thought felt very wrong not actually bad at all? Or rather, did this problem arise from bad design and I should look at this from another angle? Thanks in advance.