在 Python 中使用类实例树
我正在寻找有关处理类实例树的更多信息,以及如何最好地从树干调用叶子上的方法。我有一个主干实例,其中包含许多分支实例(在字典中),每个分支实例都有许多叶实例(以及分支中的字典)。叶子是操作真正发生的地方,因此叶子中有用于查询值、恢复值和许多其他事情的方法。
这会导致代码重复,因为我可能想对分支的所有叶子执行某些操作,因此分支中有一些方法可以对叶子、指定的一组叶子或已知的所有叶子执行某些操作分支,尽管这些确实通过简单地循环叶子并要求它们自己做所说的事情来减少代码重复(因此执行工作的实际代码位于叶子类中的一个位置)。
然后树干进来,我可能想一下子对整棵树(即所有叶子)做一些事情,所以我在那里有方法要求所有已知的对象运行它们的全叶子函数。我开始觉得这种方式与叶子中的实际操作相去甚远,尽管它工作得很好,而且代码看起来相当紧凑——非常简短、可读且功能良好。
另一个问题在于逻辑分组。我可能想将一些数据与一些、大多数或所有叶子相关联,以表明它们是某个逻辑组的一部分,因此目前叶子本身都存储此类数据。当我想要获得一个逻辑组时,我必须扫描所有叶子并将它们收集起来,而不是在主干级别拥有某种列表。这实际上一切都很好,甚至很合乎逻辑,但感觉很疯狂。这是否只是使用树状结构的本质,因为它们的复杂性,或者还有其他方法来做这些事情?我不喜欢构建二级结构来从相反的方向连接到事物 - 例如,创建一个引用逻辑组中的叶子的结构,然后从更像列表的方向接近它们。将所有东西都放在像这样的大树中的一个好处是,它可以一次性倾倒并与泡菜一起装载。
我很想听听其他人对此类事情的经验的任何想法。
I'm looking for more information about dealing with trees of class instances, and how best to go about calling methods on the leaves from the trunk. I have a trunk instance with many branch instances (in a dictionary), and each has many leaf instances (and dicts in the branches). The leaves are where the action really happens, and as such there are methods in the leaves for querying values, restoring values, and many other things.
This leads to what feels like duplication of code, as I might want to do something to all leaves of a branch, so there are methods in the branches for doing something to a leaf, a specified set of leaves, or all leaves known to that branch, though these do reduce the code duplication by simply looping over the leafs and asking them to do said things to themselves (thus the actual code doing the work is in one place in the leaf class).
Then the trunk comes in, where I might want to do something to the entire tree (i.e. all leaves) in one fell swoop, so I have methods there that ask all known objects to run their all-leaf functions. I start to feel pretty removed from the real action in the leaves this way, though it works fine, and the code seems fairly tight - extremely brief, readable, and functioning fine.
Another issue comes in logical groupings. There are bits of data I might want to associate with some, most, or all leaves, to indicate that they're part of some logical group, so currently the leaves themselves are all storing that kind of data. When I want to get a logical group, I have to scan all leaves and gather them back up, rather than having some sort of list at the trunk level. This actually all works fine, and is even pretty logical, yet it feels insane. Is this simply the nature of working with tree-like structures, because of their complexity, or are there other ways of doing these kinds of things? I prefer not to build secondary structures to connect to things from the opposite direction - e.g. making a structure with references to the leaves in a logical group, approaching them then from that more list-like direction. One bonus of keeping things all in a large tree like this is that it can be dumped and loaded in one shot with pickle.
I'd love to hear thoughts - any and all - from anyone else's experience with such things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从你的问题中得到的结论是“一切正常”,但是代码开始感觉难以管理且难以推理,并且:有没有更好的方法来做到这一点?
您的问题缺少的一件事是坚实的背景。你的树结构实际上解决了什么类型的问题?这些对象实际上有什么作用?它们都是同一类型的对象,还是混合了对象?通过其中一些细节,您可能会得到更实际的答复。
就目前情况而言,我建议查看一些有关设计模式的资源。具体来说是 composite 和 访客 模式。
在书的末尾,您可以查看 设计模式和/或 重构模式。这两本书中都没有任何 Python 代码,但如果您不介意 Java,后者是一个很好的介绍,介绍了难以推理的代码结构和使用模式来更好地组织事物。
您还可以看看 Alex Martelli 关于 Python 设计模式 的演讲。
这个问题一般有一些关于模式和Python的进一步资源链接。
希望有帮助。
What I'm taking away from your question is that "everything works", but that the code is starting to feel unmanagable and difficult to reason about, and: is there a better way to do this?
The one thing your question is missing is a solid context. What sort of problem is your tree structure actually solving? What do these object actually do? Are they all the same type of object, or is there a mix of objects? With some of these specifics you might get more practical responses.
As it stands, I would suggest checking out some resources on design patterns. Specifically the composite and visitor patterns.
On the book end of things you could have a look at Design Patterns and/or Refactoring to Patterns. Neither of these have any Python code in them, but if you don't mind Java, the latter is an excellent introduction to taking hard to reason code structures and using a pattern to better organize things.
You might also have a look at Alex Martelli's talk on Python Design Patterns.
This question has some further resource links regarding patterns and python in general.
Hope that helps.