GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - lockwo/ihoop: A lightweight package for Strict Abstract/Final Python classes

BranchesTags
Open more actions menu

Latest commit

 

History

8 Commits
8 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
examples
examples
 
 
ihoop
ihoop
 
 
tests
tests
 
 
.flake8
.flake8
 
 
.gitignore
.gitignore
 
 
.pre-commit-config.yaml
.pre-commit-config.yaml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
pyproject.toml
pyproject.toml
 
 

Repository files navigation

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
  • ?

About

A lightweight package for Strict Abstract/Final Python classes

Topics

metaclassobject-oriented-programmingpython

Resources

Readme
Apache-2.0 license
Activity

Stars

11 stars

Watchers

2 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.