小编ko_*_*_00的帖子

Python - 类 Property.Setter 不验证字典键上的直接设置

main():请参阅注释中提到的结果的函数。*一般注意事项:当Class()有一个attribute包含例如str->时,@propery.setter一旦设置就会验证。

但是,当我将 a 存储dictionary在 中时Class.attribute,@property.setter如果我直接设置 a ,则 My 不起作用key : value

class CoinJar():

    def __init__(self):
        self._priceDict = {'current' : 0.0, 'high' : 0.0, 'low' : 0.0}
        self.klines = {}
        self.volume = {}

    def __str__(self):
        return f'\n\U0001F36A'

    @property
    def priceDict(self):
        return self._priceDict

    @priceDict.setter
    def priceDict(self, priceDict):

        print('setting price')

        try:
            newPrice = float(priceDict.get('current'))
        except ValueError as e:
            print(f'Input cannot be converted to float {e}')

        # Exceptions …
Run Code Online (Sandbox Code Playgroud)

python oop dictionary python-descriptors

0
推荐指数
1
解决办法
573
查看次数

标签 统计

dictionary ×1

oop ×1

python ×1

python-descriptors ×1