Ivan Illarionov a écrit :
On 4 сент, 22:59, Carl Banks <[EMAIL PROTECTED]> wrote:
You can write code to guard against this if you want:
class A:
legal = set(["x"])
def __setattr__(self,attr,val):
if attr not in self.legal:
raise AttributeError("A object has no attribute '%s'" %
attr)
self.__dict__[attr] = val
def __init__(self,x):
self.y = x
I suspect most people who go into Python doing something like this
soon abandon it when they see how rarely it actually catches anything.
'__slots__' is better:
For which definition of "better" ? __slots__ are a mean to optimize
memory usage, not to restrict dynamism. Being able to dynamically add
arbitrary attributes is actually a feature, not a bug, and uselessly
restricting users from doing so is not pythonic. IOW : don't do that.
--
http://mail.python.org/mailman/listinfo/python-list