_Prevent_ dynamic attribute addition?

Mark McEahern marklists at mceahern.com
Fri Aug 30 19:59:33 CEST 2002

[Sean 'Shaleh' Perry]
> So in other words, if you define __slots__ *ONLY* those members 
> can exist.  
> Keen.

Yup, further, subclasses are not restricted to the base class' slots:

class A(object):

    __slots__ = ['foo']

class B(A):


a = A()
b = B()

a.foo = 1
    a.bar = 2
except AttributeError, e:
    print "What we expected:"
    print e

b.foo = 1
print "This does not raise an error"
b.bar = 2

More information about the Python-list mailing list