Skip to main content

Create class properties which can't be changed.

Project description

Python Read Only Property
=========================

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

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

read-only-property-0.1.tar.gz (3.4 kB view details)

Uploaded Source

File details

Details for the file read-only-property-0.1.tar.gz.

File metadata

File hashes

Hashes for read-only-property-0.1.tar.gz
Algorithm Hash digest
SHA256 347f28042fe95cd78ab57ab5d6ab50fc74e9757b1530e8b129b046990250183d
MD5 4a5700c0f479383036557e87b169225d
BLAKE2b-256 930ed202a0cb5ef4fce40e5285d7094a1b6d1bc33392d1116e938e834645d7fd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page