API Reference

better_sum.sum_starts_at_instance(*args, **kwargs)

Register a type's start value for sum.

Parameters:
  • args (args, default: () ) –

    Pass arguments as specified in the decorated type's positional arguments. Mutually exclusive with with_args

  • kwargs (kwargs, default: {} ) –

    Any keyword arguments as specified by the decorated type.

better_sum.sum(__iterable, *maybe_start)

A type-aware extension replacing Python's built-in sum.

To use it, first register types by doing one of the following:

  1. Use the sum_starts_at_instance decorator
  2. Provide a __sum_start__ class attribute on a type

Both of these can get tricky, but the decorator is probably easier.

Python's built-in sum usually starts the accumulator value at 0, but you can set it to anything you want with via its optional positional start argument.

Parameters:
  • __iterable (Iterable[_A]) –

    An iterable to sum the contents of.

  • __start

    An optional overriding start value, just as in the original sum.