"""
When a new class defines the __slots__ attribute, objects of the class can assign values only to attributes whose names appear in the __slots__ list. If a client attempts to assign a value to an attribute whose name does not appear in __slots__, Python raises an exception.
If a new class defines attribute __slots__, but the class’s constructor does not initialize
the attributes’ values, Python assigns None to each attribute in __slots__ when an object
of the class is created.
A derived class inherits its base-class __slots__ attribute. However, if programs should
not be allowed to add attributes to objects of the derived class, the derived class must define
its own __slots__ attribute. The derived-class __slots__ contains only the allowed
derived-class attribute names, but clients still can set values for attributes specified by the
derived class’s direct and indirect bases classes.
"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cited from Usage of __slot__
class Base:
__slots__ = 'foo', 'bar'
class Right(Base):
__slots__ = 'baz',
class Wrong(Base):
__slots__ = 'foo', 'bar', 'baz' # redundant foo and bar
No comments:
Post a Comment