Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Constants in python (See related posts)

From Alex's recipe.
# Put in const.py...:
class _const:
    class ConstError(TypeError): pass
    def __setattr__(self,name,value):
        if self.__dict__.has_key(name):
            raise self.ConstError, "Can't rebind const(%s)"%name
        self.__dict__[name]=value
import sys
sys.modules[__name__]=_const()

that's all -- now any client-code can
import const
const.magic = 23     # bind an attribute ONCE
const.magic = 88     # raises const.ConstError if re-bind


You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts