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 withAbstractAttribute[T]; subclasses must supply (via type hints) a real value.
- 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
class AbstractAnimal(Strict):
name: AbstractAttribute[str]
class Dog(AbstractAnimal):
name: str
def __init__(self, name: str):
self.name = name
>>> Dog("Fido").name
'Fido'
>>> d = Dog("Rex")
>>> d.name = "Max"
AttributeError: Cannot set attribute 'name' on frozen instance of Dog. strict objects are immutable after initialization.
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
Strictby doing things in the__init__.
Roadmap
- dataclass testing and integration
- abstractclassvar: strictness for class variables
- Package for pypi
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ihoop-0.1.1.tar.gz.
File metadata
- Download URL: ihoop-0.1.1.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d1d64851bb1182d34df000ee9f4f92e09ae3adefbb7415eb49b2aad3f165646
|
|
| MD5 |
651b6bb92bc706e5500ff717466865b3
|
|
| BLAKE2b-256 |
c25f00183fb2c62e31cdd576a1b0227bae0e5dedeec6d9bb5dc378a1f8ebe56d
|
File details
Details for the file ihoop-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ihoop-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6089aa047d0ca36d21a830d2aba014a69c7c0af6f5738dcb0f83db9feacf53ca
|
|
| MD5 |
29115eb38c7db3d8309c7e71ac82ad73
|
|
| BLAKE2b-256 |
461c16d387d8c0ce1ad375ebcc01200ecda5c89c17387b46019d38b3d3f9a9ca
|