简体   繁体   English

创建对对象属性的引用列表

[英]Creating a list of references to object attributes

The problem in the code is that Vector.numerical_vector is a list of copies of floats, but I need it to be list of references, so when Vector.vector[n].value is changed, Vector.numerical_vector[n] is changed to the same value as well. 代码中的问题是Vector.numerical_vector是浮点数的副本列表,但是我需要将其作为引用列表,因此,当Vector.vector [n] .value更改时,Vector.numerical_vector [n]更改为同样的价值。

Thanks for help in advance! 预先感谢您的帮助!

class Var:
    def __init__(self, val):
        self.value = float(val)

    def __float__(self):
        return self.value


class Vector:
    def __init__(self, vector):
        self.vector = vector
        self.numerical_vector = [float(x) for x in vector]

vars = [Var(i) for i in range(5)]
vector = Vector(vars)

You might want to employ a property for that. 您可能要为此使用一个property

class Vector:
    def __init__(self, vector):
        self.vector = vector
    @property
    def numerical_vector(self):
        return [x.value for x in self.vector]

Now vector.numerical_vector will always be synchronized. 现在, vector.numerical_vector将始终同步。 A drawback of this method is that it is recomputed each time you access it, some it may not be adapted to your specific application. 该方法的一个缺点是每次您访问它时都会重新计算它,有些可能不适合您的特定应用程序。

If performance is an issue, you could add __getitem__ and __setitem__ methods to Vector as follows: 如果性能是一个问题,可以向Vector添加__getitem____setitem__方法,如下所示:

class Vector:
    def __init__(self, vector):
        self.vector = vector
        self.numerical_vector = [float(x) for x in vector]
    def __getitem__(self, i):
        return self.vector[i]
    def __setitem__(self, i, val):
        self.vector[i] = val
        self.numerical_vector[i] = float(val)

In that case, if you set vector_instance[i] , then vector_instance.numerical_vector[i] will be also modified. 在这种情况下,如果设置vector_instance[i] ,那么还将修改vector_instance.numerical_vector[i] But if you want to modify vector_instance.vector[i] directly, then synchronicity will be broken. 但是,如果您想直接修改vector_instance.vector[i] ,则同步性将被破坏。

Depending on the use case, you can use either of the two approaches. 根据使用情况,可以使用两种方法中的任何一种。 Both have limitations but I don't think much more can be done. 两者都有局限性,但我认为没有更多的事情可以做。

There's no way to do that in python. 没有办法在python中做到这一点。 What you could do is make numerical_vector an object that, when accessed, returns the float value of the corresponding vector item. 您可以做的是使numerical_vector一个对象,该对象在访问时返回相应vector项的float值。 Eg 例如

class VectorWrapper:
    def __init__(self, vector):
        self.vector = vector

    def __getitem__(self, i):
        return float(self.vector[i])

and set self.numerical_vector = VectorWrapper(self.vector) 并设置self.numerical_vector = VectorWrapper(self.vector)

If you have to return a float representation when self.numerical_vector[i] is referenced, 如果您必须在引用self.numerical_vector[i]时返回float表示形式,

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

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