First page Back Continue Last page Overview Graphics

Slots

We can use slots to prevent the dynamic creation of attributes. That means the creation of the dictionary „__dict__“.

To define slots, you have to define a list with the name __slots__.

The list has to contain all the attributes, you want to use. After that we can‘t create additional attributes.

class S(object):

__slots__ = ['val']

def __init__(self, v):

self.val = v

x = S(42)

print(x.val)

x.new = "not possible"

Output