how to write-protect names

anton muhin antonmuhin.REMOVE.ME.FOR.REAL.MAIL at
Thu Sep 18 11:06:25 CEST 2003

python wrote:
> Can you post a trivial example of how to use __setattr__() and how to
> set the namespace in the interpreter?
> Thanks.

Really basic one:

class Namespace(object):
     def __setattr__(self, name, value):
         if name in self.__dict__:
             raise "oops"
         object.__setattr__(self, name, value)

util = Namespace()

util.x = 1
print util.x

util.x = 2

More information about the Python-list mailing list