小编Ted*_*boy的帖子

__add__两个类对象

我一直在通过搜索教程来学习Python 2。我发现有关课堂的事情。我在__add__躲闪方法上有些麻烦。我不知道如何添加一个类的2个对象。

要更清楚:

class Add_obj:
    def __init__(self, *num):
        self.num = num

    def __repr__(self):
        return 'Add_obj{}'.format(self.num)

    def __add__(self, other):
        for i in zip(*(self.num + other.num)):
            # and then some codes over here

#when:
obj1 = Add_obj(2, 5)
obj2 = Add_obj(4, 7)
# it should return Add_obj(6, 12)
Run Code Online (Sandbox Code Playgroud)

我知道这不是添加2个对象的最佳方法吗?

python class python-2.x

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

标签 统计

class ×1

python ×1

python-2.x ×1