Provides a 'classproperty' decorator.
Project description
Simple Python classproperty
decorator
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
- Import the module:
from simple_classproperty import ClassPropertyMeta, classproperty
- 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 themetaclass
! - (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
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
Close
Hashes for simple-classproperty-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b885171033095b08d84d8b94398427dfaacc45ba7491a7ef9ed61a98e6a7a07d |
|
MD5 | 3415388f6692a023483803ca73324ce3 |
|
BLAKE2b-256 | 015cf9e1675c02adad4f346d4c7706eb0eb6a1af5f3b257e39da8889359ad668 |
Close
Hashes for simple_classproperty-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46dafc4d1b3971f5a255741ec448863c8cdc57b58ad1c91c90dd0fa223d8f1c6 |
|
MD5 | 9e56f337ced747d292768f15666f622d |
|
BLAKE2b-256 | 4eddbff485d7d63c43e2f8433acfa478df8614282fa07c46c7182868d8a885ac |