Skip to main content

Abstract/Final Patterns in Python

Project description

ihoop

Strict, immutable Abstract/Final classes

Do you like python, but wish it is was more like Julia? Do like Julia, but wish it was a language other people used? Did you read Design Patterns: Elements of Reusable Object-Oriented Software, but wish you didn't?

Meet ihoop: a tiny library that turns plain Python classes into frozen, Abstract/Final instances. Drop in Strict as a base class and get:

  • Immutability by default: once __init__ finishes, your objects are read-only.
  • Abstract or Final:
    • Abstract classes: can be subclassed, but cannot be instantiated.
    • Concrete classes: can be instantiated, but cannot be subclassed.
  • Abstract attributes, not just methods: Declare with AbstractAttribute[T]; subclasses must supply (via type hints) a real value.
  • Abstract class variables: Declare with AbstractClassVar[T]; subclasses must supply a value, which becomes immutable.
  • No dependencies: pure Python.

This package was largely copied inspired by equinox's strict=True flag. For more information see https://docs.kidger.site/equinox/pattern/.

Why is it called ihoop? Because (i) (h)ate (o)bject (o)riented (p)rogramming 😉

Example

from ihoop import Strict, AbstractAttribute, AbstractClassVar

class AbstractAnimal(Strict):
    name: AbstractAttribute[str]
    species: AbstractClassVar[str]

class Dog(AbstractAnimal):
    name: str
    species = "Canis familiaris"

    def __init__(self, name: str):
        self.name = name

>>> Dog("Fido").name
'Fido'
>>> Dog.species
'Canis familiaris'
>>> d = Dog("Rex")
>>> d.name = "Max"  # Instance attributes are immutable
AttributeError: Cannot set attribute 'name' on frozen instance of Dog. strict objects are immutable after initialization.
>>> Dog.species = "Something else"  # Class variables are also immutable once resolved
AttributeError: Cannot set frozen class variable 'species' on class 'Dog'. Class variables resolved from abstract requirements are immutable.

Sharp Edges

  • It is important to type hint all member variables at the class level. It is very possible to bypass the checking/enforcement of Strict by doing things in the __init__.

Roadmap

  • dataclass testing and integration
  • abstractclassvar: strictness for class variables
  • Package for pypi
  • ?

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ihoop-0.1.2.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ihoop-0.1.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file ihoop-0.1.2.tar.gz.

File metadata

  • Download URL: ihoop-0.1.2.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ihoop-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2ed976c0534b0047ebeedf0257bacaec883358b8909411ebcfbe22509f5ce597
MD5 b2d7eb5c19f2ac82caab8a97e5cc3fc5
BLAKE2b-256 553ca2dd8bd195c8de5f89e9cf0779d6b3d71e7a431438fb4510b97ab3ec6dea

See more details on using hashes here.

Provenance

The following attestation bundles were made for ihoop-0.1.2.tar.gz:

Publisher: publish-to-pypi.yml on lockwo/ihoop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ihoop-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ihoop-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ihoop-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 20476b7a536dd769d456a3b4101f101f7425ac2672e9ddc5a2936050904326c9
MD5 623ee0f8abb70156a320fcba7ff87d4e
BLAKE2b-256 375780bf739711ab1a29830b0dce2fd108f11b5e3435e7623ef9782b8e4e8f53

See more details on using hashes here.

Provenance

The following attestation bundles were made for ihoop-0.1.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on lockwo/ihoop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page