简体   繁体   English

在对象之间链接公共类属性的方法? (不是继承)

[英]Methods to link common class attributes between objects? (not inheritance)

I have a number of objects of the same class, each holding different data. 我有许多同一类的对象,每个对象持有不同的数据。

However, they have a number of attributes that should be 'common' - ie set to the same value across all objects. 但是,它们有许多应该是“通用”的属性 - 即在所有对象中设置为相同的值。 (this could be a numeric value, a reference to another object etc etc) (这可以是数值,对另一个对象的引用等)

What are different methods of implementing this? 有哪些不同的实现方法? A couple of methods I thought of: 我想到的几种方法:

A dictionary containing all the objects and the common attributes as separate key/value pairs. 包含所有对象和公共属性的字典,作为单独的键/值对。 The problem here is that the objects are dynamically created (there is a variable number of objects read from file) so creating keys could be a problem 这里的问题是动态创建对象(从文件中读取可变数量的对象),因此创建密钥可能是个问题

A 'collection' class, where the common attributes are defined in this collection class and the group of objects is passed in as a list 一个'集合'类,其中公共属性在此集合类中定义,对象组作为列表传入

I can't help feeling that there might be alternatives out there, or 'better' methods (I know that is subjective, but I'd like to know all possibilities...) 我不禁觉得那里可能有替代品,或者“更好”的方法(我知道这是主观的,但我想知道所有可能性......)

Edit: . 编辑:

Here is my specific example. 这是我的具体例子。

I have a number of objects, each defining the branch of a river (from now on called Branches . So, they all have different x,y point attributes. 我有许多对象,每个对象定义一条河的分支(从现在开始称为Branches 。因此,它们都有不同的x,y点属性。

They should all be 'linked' to another object, in this case an object describing the elevation over the domain (from now on called DEM ) 它们应该全部“链接”到另一个对象,在这种情况下是描述域上DEM的对象(从现在开始称为DEM

One way to do it could be a 'collector' class (call it Network ) which groups together all the Branches objects and has an attribute which is a reference to the DEM. 一种方法可以是“收集器”类(称为Network ),它将所有Branches对象组合在一起,并具有一个属性,该属性是对DEM的引用。

This way, I can safely use the DEM in all calculations using Branches . 这样,我可以使用Branches在所有计算中安全地使用DEM However, it is not ideal. 但是,它并不理想。 I would like each Branches object to have a method which returns the elevation at each point. 我希望每个Branches对象都有一个返回每个点的高程的方法。 For that, DEM needs to either be passed into the Branches method (meaning I cannot define that method as a property, which I would like to do) or there needs to be an attribute within each Branches object pointing to DEM . 为此, DEM需要传递给Branches方法(意味着我不能将该方法定义为属性,我想做)或者每个Branches对象中都需要一个指向DEM的属性。 The second method runs the risk of allowing separate instances of DEM to be referenced within each Branches object, which I don't won't to allow. 第二种方法存在允许在每个Branches对象中引用DEM单独实例的风险,我不会允许这样做。 It also seems a bit silly to have each Branches object holding a separate reference to the same thing (1 global reference would be preferred - this way I can change it without having to go through each Branches object). 让每个Branches对象保持对同一事物的单独引用似乎有点愚蠢(首选1个全局引用 - 这样我可以更改它而无需遍历每个Branches对象)。

I find this kind of thing cropping up all over - I have a load of instances of the same object, but I want to keep some attributes common between them - so more general answers are preferred.. 我发现这种事情全部出现 - 我有一堆相同对象的实例,但我想保留它们之间的一些共同属性 - 所以更喜欢更常见的答案。

If you want to have attributes that are shared by and accessible to all instances of a class, use class attributes: 如果要拥有由类的所有实例共享和可访问的属性,请使用类属性:

class MyClass(object):

    myclassattr = 0 # class attribute

    def __init__(self, attr):
        self.myinstattr = attr # instance attribute
        if MyClass.myclassattr == 0:
            print("First!")
        MyClass.myclassattr += 1    

For example: 例如:

>>> a = MyClass()
First!
>>> b = MyClass()
>>> MyClass.myclassattr
2

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

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