Skip to main content

Create class properties which can't be changed.

Project description

Python Read Only Property


> 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

class MyClass:

def a(self):
return 1

The above can be shortened:

class MyClass(object):
def __init__(self, a, b, c):
self.readonly = a
Once assigned in the constructor, you can't change it.


This package installs a single Python module ``, containing a single
decorator `read_only_properties`. To use it, simply import the decorator and
decorate your classes.

from rop import read_only_properties

class Foo:
def __init__(self, a, 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
def a(self):
return self._a
def b(self):
return self._b
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].


Project details

Release history Release notifications | RSS feed

This version


Download files

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

Files for read-only-property, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size read-only-property-0.1.tar.gz (3.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page