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

2024-09-29 17:18:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个包含两个浮点的类;一个数据点和一个与该数据点相关联的错误。以下是我的类定义:

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,以及类似的简单错误表达式。我尝试使用以下函数定义(在我的类定义中)来实现这一点:

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]

举个例子数据集.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)

Tags: theinselfforreturn定义deferror
1条回答
网友
1楼 · 发布于 2024-09-29 17:18:52

第一个错误

尝试:

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

注意,创建新的DataPoint对象时不必传递'self'。你知道吗


第二个错误

您可以用str初始化数据,但您打算用float初始化它们

尝试:

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

相关问题 更多 >

    热门问题