Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 内部返回属性错误:can';t集属性_Python_Python 3.x_Unit Testing_Class_Enums - Fatal编程技术网

Python 内部返回属性错误:can';t集属性

Python 内部返回属性错误:can';t集属性,python,python-3.x,unit-testing,class,enums,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 exogenous(q1, q2, value): if type(value) == int: q2.derivative.value = DValue.add(q

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

def influencePositive(q1, q2):
    if(q1.magnitude.greaterZero()):
        q2.derivative.value = DValue.add(q2.derivative.value, 1)
def exogenous(q1, q2, value):
    if type(value) == int:
        q2.derivative.value = DValue.add(q1.derivative.value, value)
以下单元测试运行没有问题:

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 influencePositive(q1, q2):
    if(q1.magnitude.greaterZero()):
        q2.derivative.value = DValue.add(q2.derivative.value, 1)
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:无法设置属性。这就是全部回溯:

回溯(最近一次呼叫最后一次):
文件“C:/Users/Victor Zuanazzi/Documents/Artificial Intelligence/Knowledge Representation/Practicals/Qualitional\u Reaoning/relationFunction\u test.py”,第121行,在测试前增加后
r、 外源性(第一季度、第二季度、第1季度)
文件“C:\Users\Victor Zuanazzi\Documents\Artificial Intelligence\Knowledge Representation\Practicals\Quality\u Reonaning\relationFunctions.py”,第31行
q2.differential.value=DValue.add(q1.differential.value,value)
文件“C:\ProgramData\Anaconda3\lib\types.py”,第146行,在集合中__
引发AttributeError(“无法设置属性”)
AttributeError:无法设置属性
下面是一些理解上述代码的背景知识

D值是一个意图:

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
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()
工作得很好而
external()
中断。它们都以相同的方式调用
DValue.add()

在第一次测试中,导数是一个
导数
对象,可以重新分配其
属性。在第二个测试中,导数是一个
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

成功了!在你指出之后,这变得很明显,我完全看不到。是的,这有点微妙,因为两个类都有一个
.value