TypeError after overriding the __add__ method

后端 未结 5 1976
时光说笑
时光说笑 2020-11-29 03:33

I am trying to understand how __add__ works:

class MyNum:
    def __init__(self,num):
        self.num=num
    def __add__(self,other):
                 


        
5条回答
  •  青春惊慌失措
    2020-11-29 04:04

    Another option is reduce (functools.reduce in Python 3.x).

    from functools import reduce
    from operators import add
    d=[MyNum(i) for i in range(10)]
    my_sum = reduce(add,d)
    

提交回复
热议问题