Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的自适应描述符_Python_Python 3.x_Numpy_Python Descriptors - Fatal编程技术网

Python中的自适应描述符

Python中的自适应描述符,python,python-3.x,numpy,python-descriptors,Python,Python 3.x,Numpy,Python Descriptors,我想在返回代理对象的类上创建某种描述符。索引时,代理对象检索对象的成员并对其应用索引。然后返回总和 例如: 当我将实际数组作为成员变量时,此解决方案运行良好: class CompartmentCluster(Cluster): """ Base class for cluster that manages evidence. """ def __init__(self, **kwargs): super().__init__(**kwargs

我想在返回代理对象的类上创建某种描述符。索引时,代理对象检索对象的成员并对其应用索引。然后返回总和

例如:

当我将实际数组作为成员变量时,此解决方案运行良好:

class CompartmentCluster(Cluster):

    """
    Base class for cluster that manages evidence.
    """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.variable_evidence = ArraySumProxy([])

class BasicEvidenceTargetCluster(CompartmentCluster):

    # This class variable creates a Python object named basic_in on the
    # class, which implements the descriptor protocol.

    def __init__(self,
                 *,
                 **kwargs):
        super().__init__(**kwargs)

        self.basic_in = np.zeros(self.size)
        self.variable_evidence.arrays.append(self.basic_in)

class ExplanationTargetCluster(CompartmentCluster):

    """
    These clusters accept explanation evidence.
    """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.explanation_in = np.zeros(self.size)
        self.variable_evidence.arrays.append(self.explanation_in)

class X(BasicEvidenceTargetCluster, ExplanationTargetCluster):
    pass
现在我已经将数组更改为Python描述符(
cluster\u signal
实现返回numpy数组的描述符协议):


这不起作用,因为append语句追加描述符调用的结果。我需要的是附加一个绑定方法或类似的代理。修改我的解决方案的最佳方式是什么?简言之:中的变量
basic_和
中的变量
explauration_是
numpy
数组。它们现在是描述符。我想开发一些版本的
ArraySumProxy
,它可以与描述符一起工作,而不需要实际的数组。

当您访问描述符时,会对其进行评估,您只会得到值。由于描述符并不总是返回同一个对象(我想您不能回避它?),因此在初始化代理时不希望访问描述符

避免访问它的最简单方法是记住它的名称,而不是:

self.variable_evidence.arrays.append(self.basic_in)
你可以:

self.variable_evidence.arrays.append((self, 'basic_in'))
当然,
variable\u证据
必须意识到这一点,并执行
getattr(obj,name)
访问它

另一个选项是让描述符返回一个代理对象,稍后再进行计算。我不知道你在做什么,但这可能是太多的代理好品味

编辑

或者。。。您可以存储getter:

self.variable_evidence.arrays.append(lambda: self.basic_in)

我不完全理解你的问题。你能展示一下你以前是如何使用这些类的,发生了什么变化,以及在这些变化之后你希望如何使用它们吗?@brenbarn:总结。我无法从你的问题中分辨出哪些代码一直在使用,哪些代码是新的。你的代码没有显示相关部分。
cluster\u signal
是一个具有返回
numpy
数组的
\u get\u
的描述器吗?如果是,则其行为类似于
numpy
数组。如果
basic\u in
中的
basic\u未由描述符定义,那么旧代码在哪里?您是否试图让
ArraySumProxy
在索引时重新检索数组,可能是因为
中的
basic_和
属性中的
explauration_在不同的访问上对不同的数组求值?或者仅仅是
中的
lambda:self.basic_。但是,同样,一个具有获取描述符的getter的代理。。。你需要这些吗?@user2357112是的,更好:)
self.variable_evidence.arrays.append((self, 'basic_in'))
self.variable_evidence.arrays.append(lambda: self.basic_in)