Simple Python classproperty decorator
This module provides a simple way for defining class properties.
Deprecation notice: Starting with Python 3.9 the @classmethod descriptor can now wrap other descriptors like @property. See the official documentation for the announcement: https://docs.python.org/3.9/library/functions.html#classmethod
This means if you are using Python >= 3.9 you can simply realize a classproperty with:
class NewClass():
_atr = "val"
@classmethod
@property
def attr(cls):
return cls._atr
So in this case this package is not needed anymore. I will still try to maintain this package until Python 3.8 reaches EOL which will be in October 2024.
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.
Metadata
Release files for simple-classproperty 2.0.0
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-2.0.0.tar.gz | 15.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| simple_classproperty-2.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.9 kB
Release files / simple-classproperty-2.0.0.tar.gz
| Download URL | simple-classproperty-2.0.0.tar.gz |
|---|---|
| Size | 15.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b04213da5ec3ab483691de1ccf2049ee9367c212348cb5ae83d11090442a58f6
|
|
BLAKE2b-256 checksum How to use checksums |
d02eef9b35ae91fc16a3ce24a7df6f3ac915e4dd1d42716e1fbff5b3f09bbfe2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.0 CPython/3.7.13 Linux/5.15.0-1017-azure
|
Release files / simple_classproperty-2.0.0-py3-none-any.whl
| Download URL | simple_classproperty-2.0.0-py3-none-any.whl |
|---|---|
| Size | 16.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c82af9cf2450ad450d748a9259ddfe0db54886ac3b603d4aa48cf47960d04115
|
|
BLAKE2b-256 checksum How to use checksums |
00ab50644e71e064a621814b191449a12e18a25aa4077b0eb897faa04445fb6c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.0 CPython/3.7.13 Linux/5.15.0-1017-azure
|