Skip to main content

Static Class Property

Project description

static_class_property

CI

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


Download files

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

Source Distribution

static_class_property-0.0.2.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

static_class_property-0.0.2-py2.py3-none-any.whl (3.8 kB view details)

Uploaded Python 2Python 3

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

Hashes for static_class_property-0.0.2.tar.gz
Algorithm Hash digest
SHA256 114f38b9dfe12db1371c3f93c9160df0bc8d47ae05d56b4adb645db5dd34eca9
MD5 3c0e904186e7660aa7ac29a81f653f65
BLAKE2b-256 257179e5ca21a501ea6dd9f75d1d607c0717f9790f6269c30b079392b5f47a6b

See more details on using hashes here.

File details

Details for the file static_class_property-0.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for static_class_property-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0dcd63fe316713a4e072e0463366b90d16ffe27d24fc72e2c95ffdcd86b12a29
MD5 d50b56034bebb710a527936996129ce4
BLAKE2b-256 44fcaf79f0de0f13ff579958fdfb7112bd001e5dc21c9b30338ebca4db7301d4

See more details on using hashes here.

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