stop using __class__

This commit is contained in:
James Turk 2015-03-24 17:01:24 -04:00
parent bff8814fe2
commit ecf4bd7075

View File

@ -81,9 +81,9 @@ class UnitValue(object):
def as_unit(self, unit): def as_unit(self, unit):
unit = Unit.unit(unit) unit = Unit.unit(unit)
if self.unit == unit: if self.unit == unit:
return self.__class__(self.scalar, self.unit) return UnitValue(self.scalar, self.unit)
return self.__class__(self.scalar * self.unit.conversion_factor(unit), unit) return UnitValue(self.scalar * self.unit.conversion_factor(unit), unit)
def __str__(self): def __str__(self):
return '{}{}'.format(self.scalar, self.unit) return '{}{}'.format(self.scalar, self.unit)
@ -100,25 +100,25 @@ class UnitValue(object):
def __add__(self, other): def __add__(self, other):
if self.unit != other.unit: if self.unit != other.unit:
other = other.as_unit(self.unit) other = other.as_unit(self.unit)
return self.__class__(self.scalar + other.scalar, self.unit) return UnitValue(self.scalar + other.scalar, self.unit)
def __sub__(self, other): def __sub__(self, other):
if self.unit != other.unit: if self.unit != other.unit:
other = other.as_unit(self.unit) other = other.as_unit(self.unit)
return self.__class__(self.scalar - other.scalar, self.unit) return UnitValue(self.scalar - other.scalar, self.unit)
def __mul__(self, other): def __mul__(self, other):
if isinstance(other, UnitValue): if isinstance(other, UnitValue):
if self.unit != other.unit: if self.unit != other.unit:
other = other.as_unit(self.unit) other = other.as_unit(self.unit)
return self.__class__(self.scalar * other.scalar, self.unit * other.unit) return UnitValue(self.scalar * other.scalar, self.unit * other.unit)
else: else:
return self.__class__(self.scalar * other, self.unit) return UnitValue(self.scalar * other, self.unit)
def __div__(self, other): def __div__(self, other):
if isinstance(other, UnitValue): if isinstance(other, UnitValue):
if self.unit != other.unit: if self.unit != other.unit:
other = other.as_unit(self.unit) other = other.as_unit(self.unit)
return self.__class__(self.scalar / other.scalar, self.unit / other.unit) return UnitValue(self.scalar / other.scalar, self.unit / other.unit)
else: else:
return self.__class__(self.scalar / other, self.unit) return UnitValue(self.scalar / other, self.unit)