其他分享
首页 > 其他分享> > 的IntEnum返回AttributeError:无法设置属性

的IntEnum返回AttributeError:无法设置属性

作者:互联网

这是一个令人不安的问题.对于功能:

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

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

解决方法:

这是您的问题:

在您的第一个测试中,派生对象是一个派生对象,其.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))

标签:unit-testing,python-3-x,enums,python,class
来源: https://codeday.me/bug/20191210/2105255.html