JMzance
JMzance

Reputation: 1746

Python: Manipulating objects using operator overloading

I am trying to create a class which contains two floats; One data point and one error associated with that data point. Here is my class definition:

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

The operator field just holds a string and doesn't really matter for my question. My aim now is to be able to overload the '+' operator so that once I have defined two instances of my class I can simply have an expression like:

    Object3 = Object1 + Object2

Where Object3 has Datum3 = Datum1 + Datum2, and similarly simple expressions for Error. I have tried to do this by using the following function definition (inside my class definition):

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

But I get errors which basically imply I havent definined my overload correctly. Thanks in advance Jack

EDIT: The errors are things of the form

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: Small runnable example use code:

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]

Where an example DataSet.dat looks like this:

(-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)

Upvotes: 0

Views: 147

Answers (1)

Robᵩ
Robᵩ

Reputation: 168626

First Bug

Try:

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

Note that you don't have to pass 'self' when you create the new DataPoint object.


Second Bug

You initialize your data with strs, but you intend to initialize them with floats.

Try:

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

Upvotes: 4

Related Questions