classcoreΒΆ
π A Python library package which provides foundational class factories and decorators for providing classes with attributes immutability and concealment and other custom behaviors.
Key Features βΒΆ
π§ Class Customization: Composable decorators and metaclasses providing classes with customized behaviors.
π Attribute Immutability: Standard collection of metaclasses and decorators to provide immutability for class and instance attributes, with support for selective mutability via names, regexes, or predicates.
ποΈ Attribute Concealment: Standard collection of metaclasses and decorators to provide concealment for class and instance attributes, with support for selective visibility via names, regexes, or predicates.
π Dataclass Integration: Decorators like
dataclass_with_standard_behaviors
make standard Python dataclasses with
immutability and concealment. (Unlike dataclass( frozen = True )
,
attributes can still be manipulated via __post_init__
when one of these
decorators is applied.)
π§© Protocol Support: Immutable protocol classes for nominal and structural
subtyping, compatible with typing.Protocol
. Immutability and concealment
are inherited by subclassed implementations of these protocols.
ποΈ Module Reclassification: Apply immutability and concealment to entire modules.
Installation π¦ΒΆ
Method: Install Python PackageΒΆ
Install via uv pip
command:
uv pip install classcore
Or, install via pip
:
pip install classcore
Note on Immutability π’ΒΆ
Enforcement of immutability is quite difficult in Python. While this library encourages immutability by default, it can be circumvented by anyone who has intermediate knowledge of Python machinery and who is determined to circumvent the immutability. Use the library in the spirit of making programs safer, but understand that it cannot truly prevent unwanted state tampering.
Examples π‘ΒΆ
Please see the examples directory for greater detail.
Standard Behaviors πΒΆ
Produce classes which have immutable and concealed attributes and whose instances have immutable and concealed attributes. Can be normal classes, dataclasses, protocol classes, etcβ¦.
from dataclasses import field
from math import sqrt
from classcore.standard import DataclassObject
class Point2d( DataclassObject ):
x: float
y: float
hypotenuse: float = field( init = False )
def __post_init__( self ) -> None:
x, y = self.x, self.y
self.hypotenuse = sqrt( x*x + y*y )
Point2d.x = 3 # β Error, immutable class attribute.
point = Point2d( x = 3, y = 4 )
point.x = 42 # β Error, immutable instance attribute.
point.hypotenuse # Result: 5
dir( point ) # Result: ['hypotenuse', 'x', 'y']
Decorate classes so that their instances will have immutable and concealed attributes.
from dataclasses import field
from math import sqrt
from classcore.standard import dataclass_with_standard_behaviors
@dataclass_with_standard_behaviors( )
class Point2d:
x: float
y: float
hypotenuse: float = field( init = False )
def __post_init__( self ) -> None:
x, y = self.x, self.y
self.hypotenuse = sqrt( x*x + y*y )
point = Point2d( x = 5, y = 12 )
point.x = 42 # β Error, immutable instance attribute.
point.hypotenuse # Result: 13
dir( point ) # Result: ['hypotenuse', 'x', 'y']
Module Reclassification π¦ΒΆ
Make modules (or entire packages) immutable to prevent direct modification of
their attributes and to present only their public attributes via dir
.
from classcore.standard import reclassify_modules
reclassify_modules( __name__, recursive = True )
Use Cases π―ΒΆ
π Data Transfer Objects: Ensure data integrity with immutable DTOs.
ποΈ API Interfaces: Define stable, well-controlled interfaces.
π§© Plugin Systems: Plugin systems with controlled extension points.
π¦ Frameworks: Frameworks with controlled extension and modification.
Contribution π€ΒΆ
Contribution to this project is welcome! However, it must follow the code of conduct for the project.
Please file bug reports and feature requests in the issue tracker or submit pull requests to improve the source code or documentation.
For development guidance and standards, please see the development guide.
More FlairΒΆ
Table of ContentsΒΆ
- Examples
- API
- Release Notes
- Classcore 1.8 (2025-07-23)
- Classcore 1.7 (2025-07-08)
- Classcore 1.6.1 (2025-07-01)
- Classcore 1.6 (2025-07-01)
- Classcore 1.5.3 (2025-06-30)
- Classcore 1.5.2 (2025-06-29)
- Classcore 1.5.1 (2025-06-26)
- Classcore 1.5 (2025-06-24)
- Classcore 1.4.2 (2025-06-11)
- Classcore 1.4.1 (2025-06-10)
- Classcore 1.4 (2025-06-10)
- Classcore 1.3.1 (2025-06-07)
- Classcore 1.3 (2025-06-07)
- Classcore 1.2 (2025-06-05)
- Classcore 1.1 (2025-05-01)
- Classcore 1.0 (2025-04-29)
- Contribution
- License