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.3.2.tar.gz
(15.4 kB
view hashes)
Built Distribution
Close
Hashes for simple-classproperty-1.3.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dfa0ac5a0fae2998217a23bed2fea1017ccf16b04ba4f39a98fe167b3170f7c |
|
MD5 | a37ea88f084f6a449c5dee86c2ec724d |
|
BLAKE2b-256 | a7cdedb20f314918b176d3de9a29432c9021e53ad5c58f6d6c26ddab8f29ea90 |
Close
Hashes for simple_classproperty-1.3.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27918849b36e8f6726aa180d2c13b14f8cd3ae80ac17a7012053514eeb0b9c17 |
|
MD5 | e7b145583fbed00b3a94a156576da587 |
|
BLAKE2b-256 | f7281df1f8d0657e62ee2c18432627ca6dc5ecd53f1ab8d84e0441028d0338e3 |