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"
But when the value of the property is changed from an instance object, only the instance has this value:
instance = NewClass()
instance.attr = "new"
print(instance.attr) # "new"
print(NewClass.attr) # "val"
When deleting the property from the instance, the class value is used again:
instance = NewClass()
instance.attr = "new"
print(instance.attr) # "new"
del instance.attr
print(instance.attr) # "val"
Metadata
Release files for simple-classproperty 1.0.2
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.0.2.tar.gz | 3.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| simple_classproperty-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.2 kB
Release files / simple-classproperty-1.0.2.tar.gz
| Download URL | simple-classproperty-1.0.2.tar.gz |
|---|---|
| Size | 3.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e8a36fc1b48b1b1f3a770afbf2d1b1c160f3093aa07034269352872ef54bc710
|
|
BLAKE2b-256 checksum How to use checksums |
effb6b0bc4fc2020abe1beef718969ba6d41d214a56e60474045fb15fffec6c5
|
| 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.0.2-py3-none-any.whl
| Download URL | simple_classproperty-1.0.2-py3-none-any.whl |
|---|---|
| Size | 15.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
15a54f20f0c09182d39e1e6297366b4fc3d540cb5b769bf2d8d2926502626490
|
|
BLAKE2b-256 checksum How to use checksums |
06947667b909ab80114be730cd52806fa4ce3a96275db7c4b95c3ba295f75bca
|
| 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
|