简体   繁体   English

如果可以的话,在其中一个组件内访问 class 复合材料的正确方法是什么?

[英]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都会继续存在,但是没有BranchLeaf就无法有意义地存在(撇开现实生活的逻辑不谈)。 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.

相关问题 在这个 class 中测试这个“setUp()”function 的正确方法是什么? - What's the correct way to test this “setUp()” function inside this class? 在python中反转dict并访问其值的正确方法是什么? - What is the correct way to reverse dict and access its values in python? 访问指向对象的类中的成员变量的方法的正确方法是什么? - What's the correct way to access methods of a member variable in a class pointing to an object? 从类内部访问包含的静态信息的更好/正确的方法 - Better/correct way to access included static information from inside of a class 在Django中传递S3标头的正确方法是什么? - What is the correct way of passing S3 headers inside Django? 在Python中,从变量实例化类的正确方法是什么? - In Python, what's the correct way to instantiate a class from a variable? 在组件之间共享数据框的正确方法是什么? - What is the correct way for share dataframes between components? 设计调用其方法序列的 class 的最佳方法是什么? - What's the best way to design a class that calls sequences of its methods? 导入此 class 的正确方法是什么? - What is the correct way to import this class? 运行此类的正确方法是什么? - What is the correct way to run this class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM