Skip to content

Python:DataModel

Special method names

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 항목에 설명

See also

Favorite site