파이썬 클래스 인스턴스의 속성을 제한하는데 사용
- 명시된 속성만 가지기에 접근 속도 향상과 메모리 절약
- 클래스의 인스턴스가 딕셔너리를 사용하지 않아 속성추가가 제한됨
class MySlotClass:
__slot__ = ['a', 'b']
def __init(self, a: int, b: int):
self.a = a
self.b = b
- slot을 사용하지 않은 경우
- __dir__
- ['a', 'b', 'module', 'init', 'dict', 'weakref', 'doc', 'new', 'repr', 'hash', 'str', 'getattribute', 'setattr', 'delattr', 'lt', 'le', 'eq', 'ne', 'gt', 'ge', 'reduce_ex', 'reduce', 'subclasshook', 'init_subclass', 'format', 'sizeof', 'dir', 'class']
- dict
- slot을 사용한 경우
- __dir__
- ['module', 'slots', 'init', 'a', 'b', 'doc', 'new', 'repr', 'hash', 'str', 'getattribute', 'setattr', 'delattr', 'lt', 'le', 'eq', 'ne', 'gt', 'ge', 'reduce_ex', 'reduce', 'subclasshook', 'init_subclass', 'format', 'sizeof', 'dir', 'class']
- __dict__