ClassRegistry

5.2.1 · active · verified Thu Apr 16

ClassRegistry (phx-class-registry) is a Python library that implements a powerful Factory+Registry pattern for Python classes, enabling the definition of global factories that generate new class instances based on configurable keys. It supports service registries and integration with setuptools's entry_points system for extensibility. The current version is 5.2.1 and it maintains an active release cadence, supporting the three most recent Python versions.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates creating a typed ClassRegistry, registering classes using a decorator with keys, and then instantiating objects from the registry using subscript notation.

from class_registry import ClassRegistry

class Pokemon:
    pass

pokedex = ClassRegistry[Pokemon]()

@pokedex.register('fire')
class Charizard(Pokemon):
    def attack(self): return 'Flamethrower'

@pokedex.register('water')
class Squirtle(Pokemon):
    def attack(self): return 'Water Gun'

# Create instances
fire_pokemon = pokedex['fire']
water_pokemon = pokedex['water']

assert isinstance(fire_pokemon, Charizard)
assert fire_pokemon.attack() == 'Flamethrower'
assert isinstance(water_pokemon, Squirtle)
assert water_pokemon.attack() == 'Water Gun'

print(f"Created {fire_pokemon.__class__.__name__}: {fire_pokemon.attack()}")
print(f"Created {water_pokemon.__class__.__name__}: {water_pokemon.attack()}")

view raw JSON →