_Prevent_ dynamic attribute addition?

Neil Schemenauer nas at python.ca
Fri Aug 30 19:37:23 CEST 2002


Robert Oschler wrote:
> Is there a way to make a class 'non-modifiable', or am I just not
> understanding something here?

The new 2.2 __slots__ feature might help.  For example:

    >>> class A(object):
    ...     __slots__ = ['foo']
    ... 
    >>> a = A()
    >>> a.foo = 1
    >>> a.foo
    1
    >>> a.bar = 2
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    AttributeError: 'A' object has no attribute 'bar'

HTH,

  Neil




More information about the Python-list mailing list