gpt4 book ai didi

python - Python 中的自适应描述符

转载 作者:太空狗 更新时间:2023-10-30 00:16:56 30 4
gpt4 key购买 nike

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

例如,

class NDArrayProxy:

def __array__(self, dtype=None):
retval = self[:]
if dtype is not None:
return retval.astype(dtype, copy=False)
return retval


class ArraySumProxy(NDArrayProxy):

def __init__(self, arrays):
self.arrays = arrays

@property
def shape(self):
return self.arrays[0].shape

def __getitem__(self, indices):
return np.sum([a[indices]
for a in self.arrays],
axis=0)

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

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_signal 实现了返回 numpy 数组的描述符协议(protocol)):

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.

basic_in = cluster_signal(text="Basic (in)",
color='bright orange')

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

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

class ExplanationTargetCluster(CompartmentCluster):

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

explanation_in = cluster_signal(text="Explanation (in)",
color='bright yellow')

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

self.variable_evidence.arrays.append(self.explanation_in)

class X(BasicEvidenceTargetCluster, ExplanationTargetCluster):
pass

这不起作用,因为追加语句附加了描述符调用的结果。我需要的是附加一个绑定(bind)方法或类似的代理。修改我的解决方案的最佳方式是什么?简而言之:变量 basic_inexplanation_innumpy 数组。他们现在是描述符。我想开发一些使用描述符而不需要实际数组的 ArraySumProxy 版本。

最佳答案

当你访问一个描述符时,它会被评估,你只会得到值。由于您的描述符并不总是返回相同的对象(我猜您无法避免它?),因此您不想在初始化代理时访问描述符。

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

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

你这样做:

self.variable_evidence.arrays.append((self, 'basic_in'))

然后,当然,variable_evidence 必须意识到这一点并执行 getattr(obj, name) 来访问它。

另一种选择是使描述符返回一个稍后评估的代理对象。我不知道你在做什么,但这可能是太多的代理,不适合品味......

编辑

或者...您可以存储 setter/getter :

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

关于python - Python 中的自适应描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32814354/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com