Static Class Property
Project description
static_class_property
static_class_property provides a tiny @classproperty decorator for
Python classes. It lets a method be read like a property on the class while the
getter still receives the class object, making it useful for values computed
from other class attributes.
Why it exists
Python's built-in @property is for instance attributes. Stacking @property
with @staticmethod or @classmethod does not create a class-level property,
so this package supplies the descriptor needed for that pattern.
Installation
pip install static_class_property
The package declares support for Python 3.8 and newer and has no runtime dependencies.
Usage
from static_class_property import classproperty
class Settings:
env = "prod"
@classproperty
def label(cls):
return f"settings:{cls.env}"
assert Settings.label == "settings:prod"
Settings.env = "dev"
assert Settings.label == "settings:dev"
Use the getter argument like cls in a @classmethod: it is bound to the owner
class each time the attribute is read.
How it works
classproperty subclasses property and implements __get__. On access, it
wraps the original getter with classmethod, binds it to the owner class, and
calls it immediately.
Behavior and limitations
- The value is recomputed on every access; there is no caching.
- The decorator is intended for read-only computed class values.
- Assigning to the attribute on the class replaces the descriptor, as with any normal class attribute.
Development
pip install -r requirements_dev.txt
pip install -e .
make test
make lint
Docs can be built with make docs.
License
MIT License.
Changelog
0.0.1
- First release on 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 static_class_property-0.0.2.tar.gz.
File metadata
- Download URL: static_class_property-0.0.2.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
114f38b9dfe12db1371c3f93c9160df0bc8d47ae05d56b4adb645db5dd34eca9
|
|
| MD5 |
3c0e904186e7660aa7ac29a81f653f65
|
|
| BLAKE2b-256 |
257179e5ca21a501ea6dd9f75d1d607c0717f9790f6269c30b079392b5f47a6b
|
File details
Details for the file static_class_property-0.0.2-py2.py3-none-any.whl.
File metadata
- Download URL: static_class_property-0.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dcd63fe316713a4e072e0463366b90d16ffe27d24fc72e2c95ffdcd86b12a29
|
|
| MD5 |
d50b56034bebb710a527936996129ce4
|
|
| BLAKE2b-256 |
44fcaf79f0de0f13ff579958fdfb7112bd001e5dc21c9b30338ebca4db7301d4
|