【问题标题】:Is this a bug? Python property does not work correctly with +=这是一个错误吗? Python 属性不能与 += 一起正常工作
【发布时间】:2017-06-17 19:57:16
【问题描述】:

所以我今天在我的代码中遇到了这个讨厌的错误。我最近一直在使用 Python 属性,并在属性更新时使用它来将属性的旧值保存在另一个变量中。因此,当调用 setter 函数时,我需要保存旧值,然后传输新值。

事实证明,当在属性上使用+= 时,会调用__iadd__ 方法,就地更新属性,然后调用setter。但是到那个时候,旧的价值就消失了。这是一些说明问题的代码。

class Container(object):
    def __init__(self, val):
        self.val = val

    def __add__(self, other):
        print('in __add__', other)
        return Container(self.val + other.val)

    def __iadd__(self, other):
        print('in __iadd__:', other)
        self.val += other.val
        return self

    def __str__(self):
        return str(self.val)


class Test:
    def __init__(self):
        self._val = Container(0)

    @property
    def val(self):
        return self._val

    @val.setter
    def val(self, values):
        print("changing from {} to {}".format(self._val, values))
        self._val = values

test = Test()
print('val=', test.val)
test.val = Container(2)
print('val=', test.val)
test.val = test.val + Container(1)
print('val=', test.val)
test.val += Container(1)
print('val=', test.val)
test.val.__iadd__(Container(1))
print('val=', test.val)

运行时会给出以下结果:

val= 0
changing from 0 to 2
val= 2
in __add__ 1
changing from 2 to 3
val= 3
in __iadd__: 1
changing from 4 to 4
val= 4
in __iadd__: 1
val= 5

它说它正在将 4 更改为 4,但实际上是从 3 更改为 4 并且错过了。所以我想知道这是否是一个错误,我遗漏了什么,或者如果我正在处理一个属性并且需要旧值,我不应该使用 +=。

【问题讨论】:

  • 这几乎有帮助。如果我只是在对象上用__add__ 替换__iadd__,那么它会修复它。但只是我的运气__Iadd__ 是 numpy 数组的现成属性。所以我只是去掉了 setter,把它变成了一个非属性 set 函数。

标签: python properties


【解决方案1】:

棘手。您可以在 getter 中缓存旧值。这有点烦人,因为缓存操作是在每次调用 getter 时执行的,而不是在 setter 调用上。不过不管怎样……

class Container(object):
    def __init__(self, val):
        self.val = val

    def __add__(self, other):
        print('in __add__', self.val, other)
        return Container(self.val + other.val)

    def __iadd__(self, other):
        print('in __iadd__:', self.val, other)
        self.val += other.val
        return self

    def __str__(self):
        return str(self.val)

class Test:
    def __init__(self):
        self._val = Container(0)
        self._prev_val = self._val.val

    @property
    def val(self):
        self._prev_val = self._val.val
        return self._val

    @val.setter
    def val(self, values):
        print("changing from {} to {}".format(self._prev_val, values))
        self._val = values

test = Test()
print('val=', test.val)
test.val = Container(2)
print('val=', test.val)
test.val = test.val + Container(1)
print('val=', test.val)
test.val += Container(1)
print('val=', test.val)
test.val.__iadd__(Container(1))
print('val=', test.val)

输出

val= 0
changing from 0 to 2
val= 2
in __add__ 2 1
changing from 2 to 3
val= 3
in __iadd__: 3 1
changing from 3 to 4
val= 4
in __iadd__: 4 1
val= 5

【讨论】:

    猜你喜欢
    • 2022-12-11
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 2014-07-01
    相关资源
    最近更新 更多