0

我正在尝试创建一个包含两个浮点数的类;一个数据点和一个与该数据点相关的错误。这是我的类定义:

class DataPoint:
def __init__(self, Datum, Error, Operator):
    self.Datum    = Datum
    self.Error    = Error
    self.Operator = Operator
def ReturnDatum(self):
    return self.Datum
def ReturnError(self):
    return self.Error
def ReturnOperator(self):
    return self.Operator

运算符字段只包含一个字符串,对我的问题并不重要。我现在的目标是能够重载“+”运算符,这样一旦我定义了我的类的两个实例,我就可以简单地使用如下表达式:

    Object3 = Object1 + Object2

其中 Object3 有 Datum3 = Datum1 + Datum2,以及 Error 的类似简单表达式。我试图通过使用以下函数定义(在我的类定义中)来做到这一点:

def __add__(self, other):
    return DataPoint(self, self.Datum + other.Datum, math.sqrt(self.Error * self.Error + other.Error * other.Error), 'NULL')

但我得到的错误基本上意味着我没有正确定义我的重载。提前感谢杰克

编辑:错误是形式的东西

Traceback (most recent call last):
  File "DataCombination.py", line 78, in <module>
TempObject = LineData[0] - LineData[1]
  File "DataCombination.py", line 22, in __sub__
return DataPoint(self, self.Datum + other.Datum, math.sqrt(self.Error * self.Error + other.Error * other.Error), '+')
TypeError: unsupported operand type(s) for +: 'str' and 'str'

EDIT2:小型可运行示例使用代码:

import math
import sys

# Path to file and filename
FileLocation = 'DataSet.dat'

class DataPoint:
def __init__(self, Datum, Error, Operator):
    self.Datum    = Datum
    self.Error    = Error
    self.Operator = Operator
def __add__(self, other):
    return DataPoint(self.Datum + other.Datum, math.sqrt(self.Error * self.Error + other.Error * other.Error), '+')
def __sub__(self, other):
    return DataPoint(self.Datum - other.Datum, 1.0, 'NULL')
def __mul__(self, other):
    return DataPoint(self.Datum * other.Datum, 1.0, 'NULL')
def __div__(self, other):
    return DataPoint(self.Datum / other.Datum, 1.0, 'NULL')
def ReturnDatum(self):
    return self.Datum
def ReturnError(self):
    return self.Error
def ReturnOperator(self):
    return self.Operator

# Read in the data set from the file
File = open(FileLocation, 'r')
FileSegments = [line.split( ) for line in File.readlines()]

# Clean up the input
for i in xrange(FileSegments.__len__()):
for j in xrange(FileSegments[i].__len__()):
    FileSegments[i][j] = FileSegments[i][j].translate(None, '()')

# Loop over the number lines in the file
for i in xrange(FileSegments.__len__() - 2):

LineData = []

Count = (FileSegments[i].__len__() + 1) / 4

# Import strings into list of objects
for j in xrange((FileSegments[i].__len__() + 1) / 4 - 1):
    x =     j * 4
    y = 2 + j * 4
    z = 3 + j * 4
    LineData.append(DataPoint(FileSegments[i][x], FileSegments[i][y], FileSegments[i][z]))
LineData.append(DataPoint(FileSegments[i][-3], FileSegments[i][-1], 'NULL'))

TempObject = LineData[0] - LineData[1]

示例 DataSet.dat 如下所示:

(-5.63150902306 +/- 0.549562002684) * (9.62647766508 +/- 1.00395610402) + (16.9559698529 +/- 0.507466944938) + (1.07686005998 +/- 0.713190458948)
(9.40128537128 +/- 0.673031987441) * (7.65561264405 +/- 0.11828791914)
(3.19433075143 +/- 1.16442961316) / (8.49485815486 +/- 0.936343018664)
4

1 回答 1

4

第一个错误

尝试:

def __add__(self, other):
    return DataPoint(
        self.Datum + other.Datum, 
        math.sqrt(self.Error * self.Error + other.Error * other.Error), 
        'NULL')

DataPoint请注意,创建新对象时不必传递“self” 。


第二个错误

您用strs 初始化数据,但您打算用floats 初始化它们。

尝试:

def __init__(self, Datum, Error, Operator):
  self.Datum    = float(Datum)
  self.Error    = float(Error)
  self.Operator = Operator
于 2013-04-22T13:16:15.507 回答