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"
When the value of the property is changed from an instance object, the class property will be changed. All other instances will have this new value:
instance1 = NewClass()
instance2 = NewClass()
instance1.attr = "new"
print(instance1.attr) # "new"
print(instance2.attr) # "new"
print(NewClass.attr) # "new"
This behavior is the same when a property gets deleted from an instance.
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
simple-classproperty-1.4.tar.gz
(15.3 kB
view hashes)
Built Distribution
Close
Hashes for simple_classproperty-1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 860dfc449f720d3d1ae2f8aa3b5fec44cc626bd7cb566c197dc9d031d0b4b5c3 |
|
MD5 | 7704e59b98010de40effae2b3764ba51 |
|
BLAKE2b-256 | 3510fb268638b3807556edffba923589755fdb741e3466939b5d20104028df97 |