skillfed

purify

Pythonic object-mutator transforms as pure functions

purify v0.3.0 120.3K downloads/30d#12,034 on PyPI3
Permissive license Apache License Version 2.0, January 2004 https://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions… (full text in the JSON record) Active released

What it is and what it does

Purify is a decorator that solves a common Python pattern problem: writing functions that transform objects while maintaining functional purity. Normally, Python encourages mutating an object in place and returning it for efficiency, but this breaks referential transparency when the original object is needed elsewhere. Deepcopy is the pure alternative but is orders of magnitude slower. Purify uses shallow copy by default, making the decorated function pure without the performance cost of deep copying.

The decorator works by automatically shallow-copying the target argument before the function executes, so mutations inside the function only affect the copy. You specify which argument to copy either by position (last argument by default) or by name. For complex nested structures, the documentation recommends decomposing your transforms into multiple shallow-copy layers rather than using deep copy, which often yields both purer and more reusable functions.

Use it for:

  • Transform tree or graph structures where you need the original unchanged but want to write natural mutation-style code
  • Build pipelines of object modifications where intermediate results must not affect earlier stages
  • Write testable functions that don't have hidden side effects on their inputs
  • Refactor existing mutation-based codebases toward functional style without rewriting to use expensive deepcopy

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

A decorator that makes object-mutating functions pure by automatically shallow-copying the target argument, allowing you to write mutation-style code that doesn't modify the original object.

Yes, if you regularly write object-transform functions and want to enforce purity without deep-copy overhead. The package is actively maintained, has no dependencies, and works on current Python versions. It's a narrow but well-defined tool—install it when you recognize the pattern it solves in your own code, not as a general utility.

Install

purify on PyPI

pip

pip install purify

uv

uv add purify

poetry

poetry add purify

Installing purify

Before you install

Low friction: pure Python, no runtime dependencies, and supports modern Python versions 3.10 through 3.14. Repository is active with recent commits.

License in practice

Apache License 2.0 is permissive; you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.

Quickstart

pip install purify

from purify import purify

@purify
def rename_tree(name: str, tree):
    tree.name = name
    return tree

original = Tree('Old')
modified = rename_tree('New', original)
# original.name is still 'Old', modified.name is 'New'

Verify before relying

  • Whether the shallow-copy approach is sufficient for nested mutable structures in typical use cases beyond the documented examples
  • Performance characteristics and memory overhead compared to standard mutation in real-world codebases

Package facts

License Apache License Version 2.0, January 2004 https://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions… (full text in the JSON record) (permissive)
Python support supports the current Python release (<3.15,>=3.10)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 141 days since the last release
Last repo commit
First released
Downloads 120,297/month — #12,034 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: purify-0.3.0-py3-none-any.whl

Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14

Tags

pure function decorator pythonimmutable object transformsshallow copy decoratorfunctional programming object mutationpure function object modificationpythonic immutability patternobject transform without mutation
functional-programmingdecoratorimmutability

More Software Development packages