Skip to content

Python:Class

Python 에서 클래스 설계 방법.

클래스의 구조

클래스는 다음과 같은 모습이다.

class 클래스이름[(상속 클래스명)]:
    <클래스 변수 1>
    <클래스 변수 2>
    ...
    def 클래스함수1(self[, 인수1, 인수2,,,]):
        <수행할 문장 1>
        <수행할 문장 2>
        ...
    def 클래스함수2(self[, 인수1, 인수2,,,]):
        <수행할 문장1>
        <수행할 문장2>
        ...
    ...

str & repr

속성 선언 위치에 따른 주의사항

클래스 선언 직후에 하면 static-member 초기화 되는듯 <- 확인 필요

class SlotMachine:

    fullnames: Dict[str, Slot] = {}
    slots: Dict[int, Slot] = {}
    inputs: Dict[int, Slot] = {}
    outputs: Dict[int, Slot] = {}
    in_flows: Dict[int, Slot] = {}
    out_flows: Dict[int, Slot] = {}
    in_datas: Dict[int, Slot] = {}
    out_datas: Dict[int, Slot] = {}

    def __init__(self):
        pass

__init__ 함수에 넣어면 객체 멤버로 사용하는듯

class SlotMachine:
    def __init__(self):
        self.fullnames: Dict[str, Slot] = {}
        self.slots: Dict[int, Slot] = {}
        self.inputs: Dict[int, Slot] = {}
        self.outputs: Dict[int, Slot] = {}
        self.in_flows: Dict[int, Slot] = {}
        self.out_flows: Dict[int, Slot] = {}
        self.in_datas: Dict[int, Slot] = {}
        self.out_datas: Dict[int, Slot] = {}

Favorite site