Skip to main content

better-sum

Python's sum, minus ugly annotations and extra arguments.

Project goals

Quickstart

  1. Create a virtual environment with Python 3.9+
  2. pip install better-sum
  3. Try the code below
from __future__ import annotations  # Allows forward references on Python < 3.11
from typing import NamedTuple
from better_sum import sum, sum_starts_at_instance


@sum_starts_at_instance(0.0, 0.0)  # 1. Create & store a default instance
class Vec2(NamedTuple):

    x: float = 0.0
    y: float = 0.0

    def __add__(self, other: tuple[float, float]) -> Vec2:
        other_x, other_y = other
        return self.__class__(self.x + other_x, self.y + other_y)

    def __radd__(self, other: tuple[float, float]) -> Vec2:
        other_x, other_y = other
        return self.__class__(other_x + self.x, other_y + self.y)

 # 2. better_sum.sum automatically starts at the default instance
to_sum = [Vec2(0.0, 0.0), Vec2(1.0, 1.0), Vec2(2.0, 2.0)]
print(sum(to_sum))

As expected, this will print:

Vec(x=3.0, y=3.0)

Learn more in the usage documentation.

What's wrong with Python's sum?

It complicates code.

# The bad: verbose calls
sum(iterable, start_value)

# The ugly: gross type annotations
class SupportsSum:

    # int is ugly
    def __radd__(self, other: int | SupportsSum):
        if other == 0:
            return self
        ...

What's the catch?

A potential speed hit from indirection.

If it's a concern, then one of two things is true:

  1. You should be using binary-backed acceleration
  2. You're writing code for one of the following:
    • pyglet or another pure-Python project
    • fallback code for when binary acceleration isn't available

Metadata

Release files for better-sum 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for better-sum 0.1.0
File Size Uploaded Provenance Actions
better_sum-0.1.0.tar.gz 5.2 kB Without provenance Details for better_sum-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for better-sum 0.1.0
File Interpreter ABI Platform Actions
better_sum-0.1.0-py3-none-any.whl Python 3 none any Details for better_sum-0.1.0-py3-none-any.whl

Total release size: 11.1 kB

Release files / better_sum-0.1.0.tar.gz

Download URL better_sum-0.1.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
62380eb61603e3bf0366aacd7859836202b4a655e567104003d5867191dcc10d
BLAKE2b-256 checksum
How to use checksums
19c1be094f9862dd72c93ce87ae90844fd811d1f467594d3e122563b76dca0eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.2

Release files / better_sum-0.1.0-py3-none-any.whl

Download URL better_sum-0.1.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
800a8dae06b7fb5ead78fa44220e7a4d951e30760724ddd7efe277387d7b0c75
BLAKE2b-256 checksum
How to use checksums
e8fae077e51be27dcf8dc950dba9e1aecb554708baeb03dc1453fd925ef668cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.2

Security for better-sum 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page