在类中为字典正确使用getter/setter可以防止在设置字典元素时调用getter

2024-05-08 01:13:17 发布

您现在位置:Python中文网/ 问答频道 /正文

当我通过property decorator在类中设置字典的元素时,将调用@property getter。当我想让getter对输出做些什么时,这是一个问题

背景

这个题目与一个化学项目有关

我想让代码更可读、更易访问,而不是使用索引,等式new = self.species['CO2'] * fractionself.species[14] * fraction更好

我也试过了 Correct usage of a getter/setter for dictionary values 但它不能解决setter/getter问题

目前我通过分解getter来解决这个问题,定义一个get_dict函数,只允许设置整个字典

但是使用这种方法,我不能简单地通过数组(dictA:dictnew_values:numpy array)循环:

for i,value in enumerate(dictA):
    dictA[value] = new_values[i]

运行示例

class example():

    def __init__(self):
        self._propA = {'A':1,'B':0,'C':0}

    @property
    def propA(self):
        print('Getter')
        return normalize_dict(self._propA)

    @propA.setter
    def propA(self,propB:dict):
        print('Setter')
        match = list(set(propB).difference(self._propA))
        if match:
            raise ValueError('{match} is/are not part of the required input: {listing}'.format(match=match,listing=list(self._propA.keys())))
        else:
            for i,value in enumerate(propB):
                self._propA[value] = propB[value]

        return self._propA

支持代码


def normalize_dict(inquiry: dict):

    inquiry_new = {}

    try:
        for i,value in enumerate(dict(inquiry)):
            inquiry_new[value] = inquiry[value]
    except TypeError:
        error_string = 'Not a dictionary type class!'
        raise TypeError(error_string)


    for i,(valA,valB) in enumerate(inquiry_new.items()):
        if type(valB)!=float and type(valB)!=int:
            raise ValueError(valB,'is not a number')
        if float(valB) < 0:
            print ('Input is negative. They are ignored!')
            continue

    sum = 0
    for i,(valA,valB) in enumerate(inquiry_new.items()):
        if valB < 0:
            valB = 0
        sum += valB

    for i,(valA,valB) in enumerate(inquiry_new.items()):
        inquiry_new[valA] = valB/sum

    return inquiry_new

结果

main.py


test = example()
test.propA = {'A':5,'B':4,'C':1}
print(test.propA)
test.propA = { 'A':1 }
test.propA = { 'B':5 }
test.propA = { 'C':4 }
print(test.propA)
test.propA['A'] = 5
test.propA['B'] = 4
test.propA['C'] = 1
print(test.propA)

输出

Setter
Getter
{'A': 0.5, 'B': 0.4, 'C': 0.1}
Setter
Setter
Setter
Getter
{'A': 0.1, 'B': 0.5, 'C': 0.4}
Getter
Getter
Getter
Getter
{'A': 0.1, 'B': 0.5, 'C': 0.4}

想要的输出

Setter
Getter
{'A': 0.5, 'B': 0.4, 'C': 0.1}
Setter
Setter
Setter
Getter
{'A': 0.1, 'B': 0.5, 'C': 0.4}
Setter
Setter
Setter
Getter
{'A': 0.5, 'B': 0.4, 'C': 0.1}

问题

从输出中可以看到,调用的是“Getter”而不是“Setter”


Tags: intestselfnewforvaluedictsetter
1条回答
网友
1楼 · 发布于 2024-05-08 01:13:17

这里没有任何问题,您得到的输出非常有意义

您在实际输出中看到了这一点:

Getter
Getter
Getter
Getter
{'A': 0.5, 'B': 0.4, 'C': 0.1}

由于您的示例中的这些行:

test.propA['A'] = 5
test.propA['B'] = 4
test.propA['C'] = 1
print(test.propA)

你所说的“二传手传好手”是错误的,但我明白它是从哪里来的

test.propA将调用examplepropAgetter。不是因为“setter调用getter”(坦率地说,setter甚至没有被这3行调用),而是因为test.propA需要先获取底层字典,然后才能调用它的__setitem__方法

这里唯一“真正”的解决方案是我在评论中建议的更好的设计。您可以example中编写一个包装器方法,将项目直接设置在_propA上,但这就像跳过栅栏而不是穿过前门

相关问题 更多 >