Python:使用运算符重载操作对象

时间:2013-04-22 13:09:38

标签: python oop overloading

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

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)

1 个答案:

答案 0 :(得分:4)

第一个错误

尝试:

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

请注意,您不必通过自我&#39;当您创建新的DataPoint对象时。


第二个错误

您使用str初始化数据,但是打算使用float初始化它们。

尝试:

def __init__(self, Datum, Error, Operator):
  self.Datum    = float(Datum)
  self.Error    = float(Error)
  self.Operator = Operator