【发布时间】:2019-09-22 10:40:09
【问题描述】:
当我通过属性装饰器在类中设置字典元素时,将调用@property getter。当我希望 getter 对输出做一些事情时,这是一个问题。
背景
主题与化学项目有关。
我想编写一个更易读、更易于访问的代码,而不是使用索引等。 new = self.species['CO2'] * fraction 优于 self.species[14] * fraction
我也试过 Correct usage of a getter/setter for dictionary values 但它不能解决setter/getter问题。
目前我通过禁用 getter、定义 get_dict 函数并只允许设置整个字典来解决此问题。
但使用这种方法,我不能简单地通过数组循环(dictA:dict 和 new_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”。
【问题讨论】:
-
您可以创建
dict的子类并将所有验证逻辑委托给它的__setitem__。我认为这将是一个更好的设计,而不是试图在 using 类中塞满所有逻辑 -
我试过这个,如链接中所见,但在它进入新 dict 定义的
__setitem__之前(使用示例时)它无论如何都会通过getter --> 那是我的问题,我无法正常化它。所以通过NewDict+__setitem__方法,它仍然会通过getter——这很糟糕。 -
为什么不希望输出中的前两行是
Setter和Getter?示例中的第二行 (test.propA = {'A':5,'B':4,'C':1}) 调用Setter并打印Setter,第三行 (print(test.propA)) 调用Getter打印Getter -
你说得对,我错过了这个。我加了。
标签: python python-3.x decorator