Makes read only attributes for a Python class
Project description
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.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file read-only-attributes-1.2.tar.gz.
File metadata
- Download URL: read-only-attributes-1.2.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9464639fb9f3680d263c225e64234a8209364bb7f4ba43da5d8f003b9a1f3bcb
|
|
| MD5 |
ab2bf244a78bf384f13a047658121268
|
|
| BLAKE2b-256 |
938cd2423e6911e84d4dc58bae5612ae9898e5ab59554dfe11b6ca32b6099b36
|
File details
Details for the file read_only_attributes-1.2-py3-none-any.whl.
File metadata
- Download URL: read_only_attributes-1.2-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7eb4c90aa934120e376d45d6832ab24ce1f2331f8a6d47668108d9f16326ba99
|
|
| MD5 |
da9bef2fc97217c4e11f8907861e4e3c
|
|
| BLAKE2b-256 |
14b07475db8463890fc1a0b7f66aa7947de53870953f07bc623b10951a0ab8c6
|