Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:11:52+00:00 2026-06-08T09:11:52+00:00

I am working with a Python object that implements __add__ , but does not

  • 0

I am working with a Python object that implements __add__, but does not subclass int. MyObj1 + MyObj2 works fine, but sum([MyObj1, MyObj2]) led to a TypeError, becausesum() first attempts 0 + MyObj. In order to use sum(), my object needs __radd__ to handle MyObj + 0 or I need to provide an empty object as the start parameter. The object in question is not designed to be empty.

Before anyone asks, the object is not list-like or string-like, so use of join() or itertools would not help.

Edit for details: the module has a SimpleLocation and a CompoundLocation. I’ll abbreviate Location to Loc. A SimpleLoc contains one right-open interval, i.e. [start, end). Adding SimpleLoc yields a CompoundLoc, which contains a list of the intervals, e.g. [[3, 6), [10, 13)]. End uses include iterating through the union, e.g. [3, 4, 5, 10, 11, 12], checking length, and checking membership.

The numbers can be relatively large (say, smaller than 2^32 but commonly 2^20). The intervals probably won’t be extremely long (100-2000, but could be longer). Currently, only the endpoints are stored. I am now tentatively thinking of attempting to subclass set such that the location is constructed as set(xrange(start, end)). However, adding sets will give Python (and mathematicians) fits.

Questions I’ve looked at:

  • python's sum() and non-integer values
  • why there's a start argument in python's built-in sum function
  • TypeError after overriding the __add__ method

I’m considering two solutions. One is to avoid sum() and use the loop offered in this comment. I don’t understand why sum() begins by adding the 0th item of the iterable to 0 rather than adding the 0th and 1st items (like the loop in the linked comment); I hope there’s an arcane integer optimization reason.

My other solution is as follows; while I don’t like the hard-coded zero check, it’s the only way I’ve been able to make sum() work.

# ...
def __radd__(self, other):
    # This allows sum() to work (the default start value is zero)
    if other == 0:
        return self
    return self.__add__(other)

In summary, is there another way to use sum() on objects that can neither be added to integers nor be empty?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:11:54+00:00Added an answer on June 8, 2026 at 9:11 am

    Instead of sum, use:

    import operator
    from functools import reduce
    reduce(operator.add, seq)
    

    in Python 2 reduce was built-in so this looks like:

    import operator
    reduce(operator.add, seq)
    

    Reduce is generally more flexible than sum – you can provide any binary function, not only add, and you can optionally provide an initial element while sum always uses one.


    Also note: (Warning: maths rant ahead)

    Providing support for add w/r/t objects that have no neutral element is a bit awkward from the algebraic points of view.

    Note that all of:

    • naturals
    • reals
    • complex numbers
    • N-d vectors
    • NxM matrices
    • strings

    together with addition form a Monoid – i.e. they are associative and have some kind of neutral element.

    If your operation isn’t associative and doesn’t have a neutral element, then it doesn’t “resemble” addition. Hence, don’t expect it to work well with sum.

    In such case, you might be better off with using a function or a method instead of an operator. This may be less confusing since the users of your class, seeing that it supports +, are likely to expect that it will behave in a monoidic way (as addition normally does).


    Thanks for expanding, I’ll refer to your particular module now:

    There are 2 concepts here:

    • Simple locations,
    • Compound locations.

    It indeed makes sense that simple locations could be added, but they don’t form a monoid because their addition doesn’t satisfy the basic property of closure – the sum of two SimpleLocs isn’t a SimpleLoc. It’s, generally, a CompoundLoc.

    OTOH, CompoundLocs with addition looks like a monoid to me (a commutative monoid, while we’re at it): A sum of those is a CompoundLoc too, and their addition is associative, commutative and the neutral element is an empty CompoundLoc that contains zero SimpleLocs.

    If you agree with me (and the above matches your implementation), then you’ll be able to use sum as following:

    sum( [SimpleLoc1, SimpleLoc2, SimpleLoc3], start=ComplexLoc() )
    

    Indeed, this appears to work.


    I am now tentatively thinking of attempting to subclass set such that the location is constructed as set(xrange(start, end)). However, adding sets will give Python (and mathematicians) fits.

    Well, locations are some sets of numbers, so it makes sense to throw a set-like interface on top of them (so __contains__, __iter__, __len__, perhaps __or__ as an alias of +, __and__ as the product, etc).

    As for construction from xrange, do you really need it? If you know that you’re storing sets of intervals, then you’re likely to save space by sticking to your representation of [start, end) pairs. You could throw in an utility method that takes an arbitrary sequence of integers and translates it to an optimal SimpleLoc or CompoundLoc if you feel it’s going to help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Django/Python, but pseudo-code is definitely acceptable here. Working with some models that
I have this python file that I'm working on: class Range: An object that
I am working with datetime objects in python. I have a function that takes
I'm working on my first object oriented bit of python and I have the
I am currently working with Python and have been confused over the fact that
Ive been working with Python and C++ together for some time, but never tried
I'm trying to get the argparse module working in Python. My problem is that
I have an application, that needs to be working in all modern Python versions,
I am working in Python with an Email() class that I would like to
I have two classes that inherit from the same base class but do not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.