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(instance1.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
Built Distribution
Close
Hashes for simple-classproperty-1.3.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e3237ab86c486f3ba9ac66b4930ecc8c49a757236b597d4fb8349b27fb67826 |
|
MD5 | 3a7a151725ef16f0fa55bab047e98a0c |
|
BLAKE2b-256 | 72f58c34f8681e6bb19cafe573944e5712cb6e63a585be4713208a46233af9f1 |
Close
Hashes for simple_classproperty-1.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d597703afacaffc161bd026a6512129a67fc07f932c02c8b4d805ff0f5f37a94 |
|
MD5 | e330822638556a727a42132644ac63b1 |
|
BLAKE2b-256 | 028e5e932cbdf135359f26bc5e513ce4bc555d72937b9a8f82751048fac9ae19 |