简体   繁体   English

在Python中使用类实例树

[英]Working with trees of class instances in 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 (ie 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 - eg 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. 我很想听听别人从其他事物的经历中得到的想法。

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. 它们都不包含任何Python代码,但是如果您不介意Java,那么Java是很好的入门指南,介绍了难以推理的代码结构并使用模式来更好地组织事物。

You might also have a look at Alex Martelli's talk on Python Design Patterns . 您也可以看看Alex Martelli的Python Design Patterns演讲。

This question has some further resource links regarding patterns and python in general. 这个问题通常具有关于模式和python的一些其他资源链接。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM