__slots__
Dunder AttributePython 2.0+Advanced
Restricts instance attributes to a fixed set for memory savings
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
# __slots__ restricts attribute creation for memory efficiency class Point: __slots__ = ("x", "y") def __init__(self, x, y): self.x = x self.y = y p = Point(1, 2) print(p.x, p.y) try: p.z = 3 except AttributeError as e: print(f"Cannot add: {e}")
__slots__ is a special attribute that restricts instance attributes to a fixed set for memory savings.
Try in PlaygroundTags
oopintrospectioncore