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-4.0.0.tar.gz
(14.8 kB
view hashes)
Built Distribution
Close
Hashes for simple_classproperty-4.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22e83e3677b5b35b31c7a99ae86635aa6e7673b26068ddb8689797df2cf69aeb |
|
MD5 | 0daf576bde2f70eaf0ecf9b41cdc56ad |
|
BLAKE2b-256 | 58803deaeb89e4f9698a208afa1c3560d60bcda1fe9bb33cfe21c390ff19ad8c |
Close
Hashes for simple_classproperty-4.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cfc794f0703f2b3e7e64a073bf70824845de26d2840f26e40382a62c3844bfd |
|
MD5 | 4cf11f6bb173f0e4c0c4ea2729b1cc61 |
|
BLAKE2b-256 | 5095e6dcbb0201435456889f75421c7a77b37ec7915b8645f2f0a4b325cfb3c1 |