简体   繁体   English

OOP:对象参数取决于我调用方法的位置

[英]OOP: Object parameter depending on the place where I call a method

This is not a purely technical issue, I am trying to find the cleanest and most efficient solution possible.这不是一个纯粹的技术问题,我试图找到最干净、最有效的解决方案。

I am doing a tree exploration program in Python 3, and I use an object I need in each leaf of this tree.我正在用 Python 3 做一个树探索程序,我在这棵树的每一片叶子中使用了一个我需要的对象。 I want the values of the parameters of my object to be editable at different places in my program (in the different leaves), and the value to be updated in all the leaves, so I need to use the same object everywhere (pointers to the same object and not copies of the object).我希望我的对象的参数值可以在我的程序的不同位置(在不同的叶子中)进行编辑,并且在所有叶子中更新该值,所以我需要在任何地方使用相同的对象(指向相同的对象而不是对象的副本)。 However, I would like one of the parameters to depend on the leaf in which the object is located.但是,我希望参数之一取决于对象所在的叶子。

I actually need many objects like that.我实际上需要很多这样的对象。

在此处输入图片说明

So I see several options:所以我看到了几个选项:

  • use two objects instead of one: an object A which contains an object B as parameter.使用两个对象而不是一个:一个对象 A,其中包含一个对象 B 作为参数。 So I make copies of object A for each new leaf, but the object B parameter always points to the same object (so my changes on it "propagate" to all leaves).所以我为每个新叶子制作对象 A 的副本,但对象 B 参数总是指向同一个对象(所以我对它的更改“传播”到所有叶子)。 So I can create my leaf-specific parameter in object A;所以我可以在对象 A 中创建特定于叶子的参数;
  • store the leaf-specific variable separately from the object in each leaf, and send it as argument when I call a method of my object.将特定于叶子的变量与每个叶子中的对象分开存储,并在调用对象的方法时将其作为参数发送。 This is less convenient because I use a lot of objects in each leaf, so I should have some kind of dictionary containing these variables whose keys are my objects.这不太方便,因为我在每个叶子中使用了很多对象,所以我应该有某种字典包含这些变量,其键是我的对象。 The problem is that some methods of my object are called from other objects, so I have to pass this dictionary through all called objects.问题是我的对象的某些方法是从其他对象调用的,所以我必须通过所有被调用的对象传递这个字典。 It is less clean and the complexity seams greater (need to search in the dictionary every time I call a method);它不太干净,复杂性接缝更大(每次调用方法时都需要在字典中搜索);
  • Same thing, but the dictionnary of leaf-specific parameters is stored in each object and the keys are the leaves.同样的事情,但是叶特定参数的字典存储在每个对象中,键是叶。 I have to give the current leaf pointer to each method I call.我必须将当前叶指针指向我调用的每个方法。

I have never been faced to such a situation, so would like to have your opinion on these options or a better idea to write a code as clean and efficient as possible.我从来没有遇到过这种情况,所以想了解您对这些选项的看法,或者有更好的想法来编写尽可能干净和高效的代码。 Thanks for your help!谢谢你的帮助!

PS: I have about a thousand leaves and a hundred objects per leaf. PS:我有大约一千片叶子和每片叶子一百个物体。

Currently working on option 1, still not sure if it's the best one.目前正在研究选项 1,仍然不确定它是否是最好的。

Not super sure what you're asking.不太确定你在问什么。 However, maybe class variables can solve your problem.但是,也许类变量可以解决您的问题。 Each Leaf has certain instance variables, that are specific/unique to each leaf instance.每个Leaf都有特定的实例变量,这些变量对于每个叶子实例都是特定的/唯一的。 The Leaf class has a class variable which is shared among all leaf instances. Leaf类有一个在所有叶子实例之间共享的类变量。

class Leaf:

    class_variable = 0

    def __init__(self):
        from random import randint
        Leaf.class_variable = randint(0, 100)
        self.instance_variable = randint(0, 100)


number_of_leaves = 5
leaves = [Leaf() for _ in range(number_of_leaves)]

for leaf in leaves:
    print(f"class variable: {leaf.class_variable}\ninstance variable: {leaf.instance_variable}")

Output:输出:

class variable: 79
instance variable: 7
class variable: 79
instance variable: 57
class variable: 79
instance variable: 93
class variable: 79
instance variable: 2
class variable: 79
instance variable: 65
>>> 

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

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