python运算符重载案例
'' ' * 自定义一个Ration类(有理数类) * 把有理数的正负判断功能移动到分子,把分数看作数组other [ 0 ] = 分子, [ 1 ] = 分母 * 运算符重载还有 __mod__ ( self ) 求余 . __contians__ ( self, value ) 检查其成员资格 , 在本例中没体现 '' ' def gcd ( a , b ) : #考虑到正负数,需要每次递归都要把a , b取绝对值 a = abs ( a ) b = abs ( b ) if a < b : a , b = b , a if b == 0 : return a else : return gcd ( b , a % b ) class Ration : def __init__ ( self , numberator = 0 , denominator = 1 ) : #分子,分母 divisor = gcd ( numberator , denominator ) self . __numberator = ( 1 if denominator > 0 else - 1 ) * int ( numberator / divisor ) self . __denominator = int ( abs ( denominator ) / divisor ) # + - * /: