小编Hau*_*aus的帖子

间隔类 - Python

我应该为 Intervals 编写一个类,然后我需要定义加法(如何将两个间隔相加)。我已经这样做了,它的工作原理:

def __add__ (self, other):
    return Interval (self.a + other.a, self.b + other.b) 
Run Code Online (Sandbox Code Playgroud)

其中 a 和 b 是一个区间的终点。

现在我需要修改代码,以便定义间隔和数字c(浮点数或整数)之间的加法。

[a,b] + c = [a+c,b+c] 和

c + [a,b] = [a+c,b+c]。

我尝试了很多不起作用的东西,例如:

def __add__ (self, other, *args):
        if args:
            return Interval (self.a + other.a, self.b + other.b)
        else:
            return  Interval (self.a + int(number), self.b + int(number)) 
Run Code Online (Sandbox Code Playgroud)

无论我尝试什么都行不通。如果你有时间,请看一看,给我一个提示。我真的很感激!

python class intervals

1
推荐指数
1
解决办法
504
查看次数

标签 统计

class ×1

intervals ×1

python ×1