[英]What is the correct way to access a class composite inside one of its components, if that's even advisable?
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
.每个
Leaf
都属于一个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).无论是否有叶子,
Branch
都会继续存在,但是没有Branch
, Leaf
就无法有意义地存在(撇开现实生活的逻辑不谈)。 That means I can easily get every Leaf
from a Branch
through the Branch.leaves
attribute.这意味着我可以通过
Branch.leaves
属性轻松地从Branch
中获取每一片Leaf
。 If I understood composition correctly, a Branch
is the composite and every Leaf
in Branch.leaves
is one of its components?如果我正确理解了组合,
Branch
是组合, Branch.leaves
中的每个Leaf
都是它的组成部分之一?
However, no Leaf
actually knows to which Branch
it belongs to.但是,没有一个
Leaf
实际上知道它属于哪个Branch
。 So, I can't have it print "This leaf is orange and belongs to branch #14 ".所以,我不能让它打印“这片叶子是橙色的,属于分支#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
.我可以添加一个
Leaf.branch
属性并在我初始化一个新的Leaf
实例时传递它,看看一个Branch
必须已经存在才能发生,但这对我来说似乎很错误,因为它看起来像Branch
属于Leaf
。
The question is: what is the most pythonic and correct way to do what I described I can't do in my example?问题是:在我的例子中,我描述的我不能做的最pythonic和正确的方法是什么? 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.
提前致谢。
In a complex tree of views and subviews, every individual view has a pointer to the superview and to the window.在视图和子视图的复杂树中,每个单独的视图都有一个指向父视图和 window 的指针。 In a complex tree of menu items and submenus, every item has a pointer to the top menu.
在菜单项和子菜单的复杂树中,每个项目都有一个指向顶部菜单的指针。 It's really common for a child to know its parent or for an item to know its container.
一个孩子知道它的父母或一个项目知道它的容器真的很常见。 Some designs wouldn't be possible without that.
没有它,有些设计是不可能的。
In languages where you have to worry about memory management, the reference to the parent must be a weak reference, which means that the child has never to worry about the live cycle of its parent (while the contrary is not true).在必须担心 memory 管理的语言中,对父级的引用必须是弱引用,这意味着子级永远不必担心其父级的生命周期(反之则不然)。 Even in Python, the parent owns the child which it is free to create or delete when required but, obviously, the child can never create or delete its parent (which it could not do anyway since it doesn't know its own container or owner).
即使在 Python 中,父级也拥有子级,它可以在需要时自由创建或删除,但显然,子级永远无法创建或删除其父级(因为它不知道自己的容器或所有者,所以无论如何它都无法这样做) )。
So, there is a clear hierarchical relationship between the two objects, but that has nothing to do with the fact that they can or cannot know each other.因此,这两个对象之间存在明确的层次关系,但这与它们是否可以相互认识无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.