=========================
Background:
-----------
> Generally, Python programs should be written with the assumption that
> all users are consenting adults, and thus are responsible for using things
> correctly themsel0aves.
[Silas Ray][1] in Stackoverflow. Thus, generally speaking, Python doesn't hide
much. There is no notion of `private` vs. `public` in Python, hence everything
is writable.
But what if you or users of your code forget?
What if you really can't assume everyone is ?
Do we give up on having read only properties in Python?
Short introduction:
-------------------
This module allows you to make class attribute read only, with needing to
use `@property` or `__getattr__`.
Why not use `@property`?
Because it's verbose, and you need to create a function which returns
something.
```
class MyClass:
@property
def a(self):
return 1
```
The above can be shortened:
```
@read_only_properties('a')
class MyClass(object):
def __init__(self, a, b, c):
self.readonly = a
```
Once assigned in the constructor, you can't change it.
Usage:
------
This package installs a single Python module `rop.py`, containing a single
decorator `read_only_properties`. To use it, simply import the decorator and
decorate your classes.
```
from rop import read_only_properties
@read_only_properties('b')
class Foo:
def __init__(self, a, b):
self.a
self.b
```
Moving from @property:
----------------------
If you have a class with many attributes that you want to
refactor to properties:
```
class AClassWithManyAttributes:
def __init__(a, b, c, d, e ...)
self.a = a
self.b = b
self.c = c
``` ....
The above class would be very verbose (an IDE will save you a lot of
typing, but it won't make the code shorter:
```
class AClassWithManyAttributes:
'''refactored to properties'''
def __init__(a, b, c, d, e ...)
self._a = a
self._b = b
self._c = c
@property
def a(self):
return self._a
@property
def b(self):
return self._b
@property
def b(self):
return self._c
# you get this ... it's long
```
Now imagine you can simply do that:
```
@read_only_properties('a', 'b', 'c')
class AClassWithManyAttributes:
def __init__(a, b, c, d, e)
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
```
This makes the attributes `a, b, c` read only, trying to re-assign a value
to any of them will raise an exception, other class attribute stay unaffected.
The code for this module originated from the
[author's answer in stackoverflow][2].
[1]: http://stackoverflow.com/a/14594174/492620
[2]: http://stackoverflow.com/a/35906068/492620
Metadata
Release files for read-only-property 0.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 | Provenance | Actions |
|---|---|---|---|---|
| read-only-property-0.1.tar.gz | 3.4 kB | Without provenance | Details for read-only-property-0.1.tar.gz |
Release files / read-only-property-0.1.tar.gz
| Download URL | read-only-property-0.1.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
347f28042fe95cd78ab57ab5d6ab50fc74e9757b1530e8b129b046990250183d
|
|
BLAKE2b-256 checksum How to use checksums |
930ed202a0cb5ef4fce40e5285d7094a1b6d1bc33392d1116e938e834645d7fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for read-only-property 0.1
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.