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.1.tar.gz
(15.3 kB
view hashes)
Built Distribution
Close
Hashes for simple-classproperty-1.4.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f5bad2140aec61492ef430c957c3d848ebf548f5c03d6af2b7daa45726b739a |
|
MD5 | edea49637d1ced80ea4605560d0158c4 |
|
BLAKE2b-256 | ef663cea62dd10bf34acef800b7d8fa2c1730b95a12573d813357814fe16059a |
Close
Hashes for simple_classproperty-1.4.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0235fe1e17bca89730cdfdd8e696ac974d5271a166f11dae0fbfdda2cda9df89 |
|
MD5 | 61716605b2f015fac8f1a24a0526681d |
|
BLAKE2b-256 | 3109335b352a1af5e00502776b69e1febaf9509abdf19e3d359baa7ea47cafc2 |