How to prevent illegal definition of a variable in objects?

Daehyok Shin sdhyok at email.unc.edu
Wed Jun 14 17:48:45 EDT 2000


How can I prevent illegal definition of a variable to an object?
For instance,

class A:
    __init__(self):
            self.x = 1

    __setattr__(self, name, value):
            if name == "x":
                    self.x = value
            else:
                raise AttributeException

    __getattr__(self, name):
            if name == "x":
                return self.x
            else:
                raise AttributeException

>>> a = A()
>>> a.x = 10
>>> a.y = 20 # I want to raise an AttributeException.

Peter





More information about the Python-list mailing list