Hello, > this is probably just a silly Python question, but let me try. I've > designed a little class that has a __add__() method, and it works fine > for expressions like x+y. However, sum() does not work on a list of > such objects ! do I need to do something else ? (in the meanwhile i go > reduce(lambda x,y : x+y, list) which is cumbersome).
The sum function starts off with the integer 0, so it tries to add one of your objects to the integer 0. This is probably what's going wrong. You can pass as a second argument the thing you want sum to start with. For example, sage: sum([ [1,2,3], [4,5,6] ]) Traceback (most recent call last) ... TypeError: unsupported operand type(s) for +: 'int' and 'list' sage: sum([ [1,2,3], [4,5,6] ], []) [1, 2, 3, 4, 5, 6] > > along the same lines, my class has a __str__() method, and 'print x' > works. But 'print L' where L is a list of such objects does not > work... > > any idea? You need to define the __repr__ method, since that is what list is using to display your objects. --Mike --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---