Skip to content
Search

Python:Class

lastmod 2021-04-02created 2015-03-245 edits

How to design classes in Python.

Class structure

A class looks like this.

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

str & repr

Caveats depending on where attributes are declared

Declaring them right after the class declaration seems to initialize them as static members <- needs verification

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

Putting them in the __init__ function seems to make them object members

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

author Zer0 sitemap