IntEnum返回AttributeError:无法设置属性

时间:2019-04-07 10:34:59

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

那是一个令人不安的问题。对于功能:

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)

它引发一个Atribute错误:AttributeError:无法设置属性。那就是整个追溯:

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)

我们使用它来设置派生类:

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

数量是一类,具有为其实例设置的数量和微分值:

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()

1 个答案:

答案 0 :(得分:2)

这是您的问题:

在您的第一次测试中,派生对象是Derivative对象,可以重新分配其.value属性。在您的第二个测试中,派生对象是DValueIntEnum)对象,无法重新分配其.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))