ReadOnly.py

#!/usr/bin/env python

"""
$Id: ReadOnly.py 905 2003-04-10 05:11:01Z david $

This is a simple module that allows you to emulate the const operator
in Python using the ReadOnly class.

It works by allowing you to set the value of an attribute of a ReadOnly
object once, and only once.
"""


ERR_READONLY = "value is read-only"


class ReadOnly:
	"""
	This class does not allow the values of its attributes to be
	set more than once.  Hence, it can be used to emulate "const".
	"""
	def __setattr__ (self, name, value):
		if self.__dict__.has_key(name):
			raise TypeError, ERR_READONLY
		self.__dict__[name] = value