【问题标题】:IntEnum returning AttributeError: can't set attributeIntEnum 返回 AttributeError:无法设置属性
【发布时间】:2019-04-07 10:34:59
【问题描述】:

这是一个令人不安的问题。对于函数:

def influencePositive(q1, q2):
    if(q1.magnitude.greaterZero()):
        q2.derivative.value = DValue.add(q2.derivative.value, 1)

以下单元测试运行没有问题:

def test_i_plus_active(self):
        q1 = Quantity(Magnitude(MValue.PLUS), None)
        q2 = Quantity(None, Derivative(DValue.ZERO))

        r.influencePositive(q1, q2)
        self.assertEqual(q2.derivative.value, DValue.PLUS)

但是对于这个其他功能:

def exogenous(q1, q2, value):
    if type(value) == int:
        q2.derivative.value = DValue.add(q1.derivative.value, value)

以下单元测试中断:

def test_ex_increase_minus(self):
        q1 = Quantity(None, DValue.MINUS)
        q2 = Quantity(None, DValue.MINUS)

        r.exogenous(q1, q2, 1)
        self.assertEqual(q2.derivative.value, DValue.ZERO)

它会引发属性错误:AttributeError: can't set attribute。这就是整个回溯:

Traceback (most recent call last):
  File "C:/Users/Victor Zuanazzi/Documents/Artificial Intelligence/Knowledge Representation/Practicals/Qualitative_Reaoning/relationFunction_test.py", line 121, in test_ex_increase_minus
    r.exogenous(q1, q2, 1)
  File "C:\Users\Victor Zuanazzi\Documents\Artificial Intelligence\Knowledge Representation\Practicals\Qualitative_Reaoning\relationFunctions.py", line 31, in exogenous
    q2.derivative.value = DValue.add(q1.derivative.value, value)
  File "C:\ProgramData\Anaconda3\lib\types.py", line 146, in __set__
    raise AttributeError("can't set attribute")
AttributeError: can't set attribute

这里有一些了解上面代码的背景知识。

DValue 是一个 IntEnum:

from enum import IntEnum

class DValue(IntEnum):
    MINUS = -1
    ZERO = 0
    PLUS = 1

    @staticmethod
    def add(dvalue, delta):
        return max(min(dvalue + delta, DValue.PLUS), DValue.MINUS)

我们用它来设置类 Derivative:

class Derivative:
    def __init__(self, value):

        #if value is type int, it is converted to Enum.
        if value is int:
            value = DValue(value)

        self.value = value

Quantity 是一个类,它的实例具有大小和导数值:

from magnitude import Magnitude, MValue
from derivative import Derivative, DValue

class Quantity:
    def __init__(self, magnitude, derivative):
        self.magnitude = magnitude
        self.derivative = derivative

我不明白为什么influencePositive() 工作正常而exogenous() 中断。他们都以相同的方式调用DValue.add()。

【问题讨论】:

    标签: python python-3.x unit-testing class enums


    【解决方案1】:

    这是你的问题:

    在您的第一次测试中,派生是一个Derivative 对象,可以重新分配其.value 属性。在您的第二个测试中,派生是 DValue (IntEnum) 对象,其 .value 属性无法重新分配。

    In [4]: d = Derivative(DValue.ZERO)
    
    In [5]: d.value
    Out[5]: <DValue.ZERO: 0>
    
    In [6]: d.value = 1
    
    In [7]: d.value
    Out[7]: 1
    
    In [8]: d = DValue.MINUS
    
    In [9]: d.value
    Out[9]: -1
    
    In [10]: d.value = 1
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-10-3c0164b4d46d> in <module>()
    ----> 1 d.value = 1
    
    /home/ethan/.local/lib/python2.7/site-packages/enum/__init__.pyc in __set__(self, instance, value)
         54 
         55     def __set__(self, instance, value):
    ---> 56         raise AttributeError("can't set attribute")
         57 
         58     def __delete__(self, instance):
    
    AttributeError: can't set attribute
    

    所以我认为你的第二个测试应该像这样设置q2:

    q2 = Quantity(None, Derivative(DValue.MINUS))
    

    【讨论】:

    • 成功了!在你指出之后就很明显了,我完全看不到它。
    • 是的,这有点微妙,因为两个类都有.value!
    猜你喜欢
    • 2015-02-08
    • 2023-02-08
    • 2014-08-09
    • 2017-07-17
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 2021-04-04
    相关资源
    最近更新 更多