Python:DataModel
Special method names
- 3. Data model — Python 3.10.6 documentation # 3.3. Special method names
- (번역) 파이썬 매직 메소드 (Python's Magic Methods) | ziwon.github.io
A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names.
__new__
, __add__
같은 것들 ...
str
slots
그러나 알려진 속성을 가진 소규모 클래스의 경우 병목 현상이 발생할 수 있습니다. dict형은 메모리를 많이 낭비합니다. 파이썬은 객체 생성시에 모든 속성들을 저장하기 위해 메모리에 고정된 양을 할당할 수 없습니다. 그러므로 많은 양의 객체를 생성 하면 많은 RAM을 낭비하게됩니다. 이 문제를 우회하는 방법이 있습니다. __slots__
을 사용해서 파이썬이 dict형을 사용하지 않도록 하고 고정된 속성의 집합에만 메모리를 할당하도록 합니다.
class MyClass(object):
__slots__ = ('name', 'class')
def __init__(name, class):
self.name = name
self.class = class
self.set_up()
# ...
enter/exit
자세한 내용은 Python:With 항목에 설명