[Python] Object class __slots__를 이용한 성능 개선
·
Python/Python
[Python] Object class __slots__를 이용한 성능 개선 0. 개요 Python에서는 Object attribute에 대해서 메모리는 더 적게 사용, 접근 속도는 더 빠르게 하는 방법이 있습니다. 바로, __slots__ 를 사용하는 방법 입니다. 기본적으로 Python은 객체 인스턴스 속성을 Dict를 사용 생성하며 Dict 형은 메모리를 추가적으로 필요로 합니다. slots 을 사용 하는 경우 class는 __dict__, __weakref__ 생성을 하지 않습니다. It restricts the valid set of attribute names on an object to exactly those names listed. Since the attributes are now fi..
[Python] __slots__ method
·
Python
slots 기본적으로 파이썬에서는 객체의 인스턴스 속성을 저장하기 위해서 dict 를 사용 하는데 이를 통해 런타임 중 속성을 변경할 수 있다. 하지만 dict 는 메모리를 낭비하는 경향이 있다. slots를 사용하는 경우 두가지 효과가 있다. 속성에 대한 빠른 접근 class Normal(object): pass class UsingSlots(object): __slots__ = ['name'] normal = Normal() use_slots = UsingSlots() def fn_set_get_delete(cls): def set_get_delete(): setattr(cls, 'name', 'foo var') getattr(cls, 'name&#3..