previous next Up Title Contents Index

Advanced


Create constants

If you're willing to use something like const.foo, it's not hard to ensure its value won't be changeable:

-- in const.py:
class Const:
    foo = 'Foobar!'
    bar = 23
    pi = 3.1415926
    baz = "Whacka-doo"
    def __setattr__(self,name,value):
        if Const.__dict__.has_key(name):
            raise AttributeError, "cannot change const.%s" % name
        Const.__dict__[name] = value
const = Const()
-- anywhere else:
from const import const


# may now use const.foo, const.bar, ..., and also add new
# const.whatever values by assignment, but NOT change
# any existing one
- Alex Martelli


previous next Up Title Contents Index

Version : 1.65 Mise à jour : 11/11/00