How to prevent illegal definition of a variable in objects?

Roland Schlenker rol9999 at attglobal.net
Thu Jun 15 08:05:48 EDT 2000


How about this;

>>> class A:
...     def __init__(self):
...         self.x = 1
...     def __setattr__(self, name, value):
...         if name == "x":
...             self.__dict__[name] = value
...         else:
...             raise AttributeError
...
>>> a = A()
>>> a.x = 10
>>> a.y = 20
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 8, in __setattr__
AttributeError

Roland Schlenker




More information about the Python-list mailing list