可以强制最大强制只返回一个浮点数

时间:2018-01-03 23:28:37

标签: python python-3.x

我有一个有两个属性(索引和值)的类。我希望能够调用max并仅返回值(这是一个浮点数)。

p = [Point(1, 23.33), Point(2, 12.3)]
max(p)
#<Point object at 0x1052d4eb8> 
# I would like to see 23.33
# float(max(p)) returns the float but I want to be able to use the max function by it self

数据模型中是否有一个方法可以覆盖以强制max只返回浮点数?整个班级如下。

Python 3.6

class Point:                                                                                                                                                                                                                                                                                                                
      """ a set of data indexed by time """                                                                                                                                                                                                                                                                                   
      def __init__(self, index, value):                                                                                                                                                                                                                                                                                       
          self._index = index                                                                                                                                                                                                                                                                                                 
          self._value = value                                                                                                                                                                                                                                                                                                 

      def __float__(self):                                                                                                                                                                                                                                                                                                    
          return float(self._value)                                                                                                                                                                                                                                                                                           

      def __add__(self, other):                                                                                                                                                                                                                                                                                               
          return float(self._value) + other                                                                                                                                                                                                                                                                                   

      def __radd__(self, other):                                                                                                                                                                                                                                                                                              
          return self.__add__(other)                                                                                                                                                                                                                                                                                          

      def __lt__(self, other):                                                                                                                                                                                                                                                                                                
          return self._value < float(other)                                                                                                                                                                                                                                                                                   

      def ___le__(self, other):                                                                                                                                                                                                                                                                                               
          return self._value <= float(other)                                                                                                                                                                                                                                                                                  

      def __eq__(self, other):                                                                                                                                                                                                                                                                                                
          return self._value == float(other)                                                                                                                                                                                                                                                                                  

      def __ne__(self, other):                                                                                                                                                                                                                                                                                                
          return self._value != float(other)                                                                                                                                                                                                                                                                                  

      def __gt__(self, other):                                                                                                                                                                                                                                                                                                
          return self._value > float(other)                                                                                                                                                                                                                                                                                   

      def __ge__(self, other):                                                                                                                                                                                                                                                                                                
          return self._value >= float(other) 

1 个答案:

答案 0 :(得分:1)

根据documentation,您可以使用任何可迭代:例如,您可以迭代这样的值:

points = [Point(1, 23.33), Point(2, 12.3)]
m = max(p._value for p in points)

或使用float进行迭代:

m = max(float(p) for p in points)