通过在 python 中继承创建自定义浮点数 class
Creating a custom float class by inheritance in python
我正在尝试通过继承浮点数 class 并重载与其关联的运算符,在 Python 中创建自定义浮点数 class 'Float'。我正在重载与 class 关联的算术运算符,以便获得 decimal.Decimal class 的算术计算精度,同时保持 float class 的性能远优于 decimal.Decimal class.
这样,每当我需要显示人类可读的输出时,我就不需要使用 round() 函数或字符串格式化。
这是我的代码,仅重载了加法运算符:
class Float(float): #custom float class
def Float(self,value): #Constructor like method
self.value = value
def count_decimal_places(float_num): #Counts decimal places for rounding
if type(float_num) == int:
return 0
return len(str(float_num).split('.')[1])
def __add__(self, other): #rounds results to the higher number of decimal places
return round(self.value +other.value, max(self.count_decimal_places(self.value), self.count_decimal_places(other.value))
fo = Float()
fo.Float(3.6)
fo2 = Float()
fo2.Float(5.6)
print(fo + fo2)
当我直接在 class 中使用构造函数为 'value' 属性赋值时,这不起作用。
class Float(float):
def init(self,value):
self.value = value
def count_decimal_places(float_num):
if type(float_num) == int:
return 0
return len(str(float_num).split('.')[1])
def __add__(self, other):
return round(self.value +other.value, max(self.count_decimal_places(self.value), self.count_decimal_places(other.value))
fo = Float(3.6)
fo2 = Float(5.6)
print(fo + fo2)
它产生了语法错误。这就是为什么我在第一个代码中使用类似构造函数的方法。
此外 init 方法存在于 class float in Python 并出现在 print(dir(float)) 中,但我无法确定它的作用所以我没有使用 class float.
的预定义构造函数
您在 def __add__(self, other)
的 return 行有不匹配的括号。修好后,我把构造函数放回去了。您还忘记将 self
作为 count_decimal_places
中的第一个参数,所以我也修复了它。虽然我没有检查你函数的逻辑,但到目前为止它似乎工作正常:
class Float(float): #custom float class
def __init__(self,value): #Constructor like method
self.value = value
def count_decimal_places(self, float_num): #Counts decimal places for rounding
if type(float_num) == int:
return 0
return len(str(float_num).split('.')[1])
def __add__(self, other): #rounds results to the higher number of decimal places
return round(self.value +other.value, max(self.count_decimal_places(self.value), self.count_decimal_places(other.value)))
fo = Float(3)
fo2 = Float(5.6)
print(fo+fo2)
Output: 8.6
我正在尝试通过继承浮点数 class 并重载与其关联的运算符,在 Python 中创建自定义浮点数 class 'Float'。我正在重载与 class 关联的算术运算符,以便获得 decimal.Decimal class 的算术计算精度,同时保持 float class 的性能远优于 decimal.Decimal class.
这样,每当我需要显示人类可读的输出时,我就不需要使用 round() 函数或字符串格式化。
这是我的代码,仅重载了加法运算符:
class Float(float): #custom float class
def Float(self,value): #Constructor like method
self.value = value
def count_decimal_places(float_num): #Counts decimal places for rounding
if type(float_num) == int:
return 0
return len(str(float_num).split('.')[1])
def __add__(self, other): #rounds results to the higher number of decimal places
return round(self.value +other.value, max(self.count_decimal_places(self.value), self.count_decimal_places(other.value))
fo = Float()
fo.Float(3.6)
fo2 = Float()
fo2.Float(5.6)
print(fo + fo2)
当我直接在 class 中使用构造函数为 'value' 属性赋值时,这不起作用。
class Float(float):
def init(self,value):
self.value = value
def count_decimal_places(float_num):
if type(float_num) == int:
return 0
return len(str(float_num).split('.')[1])
def __add__(self, other):
return round(self.value +other.value, max(self.count_decimal_places(self.value), self.count_decimal_places(other.value))
fo = Float(3.6)
fo2 = Float(5.6)
print(fo + fo2)
它产生了语法错误。这就是为什么我在第一个代码中使用类似构造函数的方法。
此外 init 方法存在于 class float in Python 并出现在 print(dir(float)) 中,但我无法确定它的作用所以我没有使用 class float.
的预定义构造函数您在 def __add__(self, other)
的 return 行有不匹配的括号。修好后,我把构造函数放回去了。您还忘记将 self
作为 count_decimal_places
中的第一个参数,所以我也修复了它。虽然我没有检查你函数的逻辑,但到目前为止它似乎工作正常:
class Float(float): #custom float class
def __init__(self,value): #Constructor like method
self.value = value
def count_decimal_places(self, float_num): #Counts decimal places for rounding
if type(float_num) == int:
return 0
return len(str(float_num).split('.')[1])
def __add__(self, other): #rounds results to the higher number of decimal places
return round(self.value +other.value, max(self.count_decimal_places(self.value), self.count_decimal_places(other.value)))
fo = Float(3)
fo2 = Float(5.6)
print(fo+fo2)
Output: 8.6