Read Only Attributes
Info:
This package makes class attributes read only.
Motivation:
We can make read only attributes using @property. But this can be verbose. Worse, the intent is not clear as in these attributes are meant to be read-only.
Like as below
class MyClass:
@property
def x(self):
return 'immutable 1'
@property
def y(self):
return 'immutable 2'
@property
def z(self):
return 'immutable 3'
@property
def w(self):
return 'immutable '
The above can be written like so which is much less verbose and lot more explicit:
@read_only_attributes('x','y','z','w')
class MyClass:
def __init__(self, x, y, z, w):
self.x = x
self.y = y
self.z = z
self.w = w
Once the instance attributes are assigned in the _init_, they cannot be changed. Trying to change them will raise an AttributeError.
Installation:
pipenv install read-only-properties
Usage:
import class decorator @read_only_attributes and use like so:
from roa import read_only_attributes
@read_only_attributes('x', 'y')
class MyClass:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
That's it. There us no need to use @property.
'x' and 'y' are now readonly attributes. If we try to change them, AttributeError exception will be raised.
Since 'z' is not in decorator argument list, self.z is a mutable instance attribute.
Metadata
Release files for read-only-attributes 1.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 | Provenance | Actions |
|---|---|---|---|---|
| read-only-attributes-1.2.tar.gz | 15.2 kB | Without provenance | Details for read-only-attributes-1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| read_only_attributes-1.2-py3-none-any.whl | Python 3 | none | any | Details for read_only_attributes-1.2-py3-none-any.whl |
Total release size: 19.4 kB
Release files / read-only-attributes-1.2.tar.gz
| Download URL | read-only-attributes-1.2.tar.gz |
|---|---|
| Size | 15.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9464639fb9f3680d263c225e64234a8209364bb7f4ba43da5d8f003b9a1f3bcb
|
|
BLAKE2b-256 checksum How to use checksums |
938cd2423e6911e84d4dc58bae5612ae9898e5ab59554dfe11b6ca32b6099b36
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9
|
Release files / read_only_attributes-1.2-py3-none-any.whl
| Download URL | read_only_attributes-1.2-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7eb4c90aa934120e376d45d6832ab24ce1f2331f8a6d47668108d9f16326ba99
|
|
BLAKE2b-256 checksum How to use checksums |
14b07475db8463890fc1a0b7f66aa7947de53870953f07bc623b10951a0ab8c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9
|
Security for read-only-attributes 1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.