Skip to main content

Provides a 'classproperty' decorator.

Project description

Simple Python classproperty decorator

PyPI package PyPI version

This module provides a simple way for defining class properties.

Install

You can install this Python module via pip:

pip install simple-classproperty

Otherwise the module can be downloaded from PyPI: https://pypi.org/project/simple-classproperty/

Usage

  1. Import the module:
    from simple_classproperty import ClassPropertyMeta, classproperty
    
  2. Create a class with a class property:
    class NewClass(metaclass=ClassPropertyMeta):
        _attr = "val"
    
        @classproperty
        def attr(cls):
            return cls._attr
    
    Don't forget to set the metaclass!
  3. (Optional) Define also a setter and deleter for the newly created class property (this works like the standard python property):
    @attr.setter
    def attr(cls, value):
        cls._attr = value
    
    @attr.deleter
    def attr(cls):
        del cls._attr
    

Tips

The classproperty is also accessible from an instance:

instance = NewClass()
print(instance.attr)  # "val"

But when the value of the property is changed from an instance object, only the instance has this value:

instance = NewClass()
instance.attr = "new"
print(instance.attr)  # "new"
print(NewClass.attr)  # "val"

When deleting the property from the instance, the class value is used again:

instance = NewClass()
instance.attr = "new"
print(instance.attr)  # "new"

del instance.attr
print(instance.attr)  # "val"

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

simple-classproperty-1.0.1.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

simple_classproperty-1.0.1-py3-none-any.whl (15.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page