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.
Metadata
Release files for simple-classproperty 1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| simple-classproperty-1.1.tar.gz | 3.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| simple_classproperty-1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.5 kB
Release files / simple-classproperty-1.1.tar.gz
| Download URL | simple-classproperty-1.1.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d5bd04ce0926f2afa549fca7ef1137fafa6cd2ee2fd07780e37807260cad1ee4
|
|
BLAKE2b-256 checksum How to use checksums |
8dbfac1cea9fed562ade628ad0dae3a2ff42cac8267d53d6e7e4c8aea0d5737b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.5.9
|
Release files / simple_classproperty-1.1-py3-none-any.whl
| Download URL | simple_classproperty-1.1-py3-none-any.whl |
|---|---|
| Size | 15.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
96ded4338e64837e7b3bc0d281d72b3f28480646452955ca419f8e679245f2ba
|
|
BLAKE2b-256 checksum How to use checksums |
96271eb0334806d13cd7738729cfcdb07c23958fbd0c733809a0d909b623e613
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.5.9
|