Skip to content
Kyung-Su Kang edited this page May 21, 2024 · 2 revisions

Naming variables

파이썬/C++/JavaScript 코딩 스타일 가이드

들여쓰기

스페이스(space)와 탭(tab)을 섞어서 사용하지 않는다. VSCode를 사용할 시 탭의 스페이스 길이는 4이다.

문장의 종료

한 줄에 하나의 문장만 허용한다. 한 줄은 최대 79자까지지만 파이토치, PyQT 사용 시 flake8 옵션으로 100자까지 늘린다.

명명 규칙

클래스 생성 시 파스칼 표기법을 사용한다.

class PythonClass(SuperClass) -> None:
    """This is an example."""
    def __init__(self):
        super().__init__()
        pass    

상수는 대문자 스네이크 표기법을 사용한다.

SYMBOLIC_CONSTANT: int = 1

변수와 함수는 스네이크 표기법을 사용한다.

문자열 할당 시 작은따옴표를 사용한다.

variable_name: str = ''

def is_variable(variable: List):
    pass

지역변수 또는 private 변수(속성)/함수는 언더바(underbar)로 시작한다.

_num_images: int = 128

def _is_number(num: Any):
    pass

파이썬 기본 명령어 또는 외부 명령어 충돌을 피하기 위해 언더바를 문자열 마지막에 사용한다.

input_: Tensor = torch.Tensor()

C/C++ 확장 모듈은 밑줄로 시작한다.

전역 변수

^^

선언, 할당 그리고 모듈

한 줄에 하나씩만 선언한다.

# Wrong
a, b, = 10, 'ten'
# Correct:
spam(ham[1], {eggs: 2})

# Wrong:
spam( ham[ 1 ], { eggs: 2 } )

# Correct:
foo = (0,)

# Wrong:
bar = (0, )

# Correct:
x = 1
y = 2
long_variable = 3

# Wrong:
x             = 1
y             = 2
long_variable = 3
income = (gross_wages +
          taxable_interest +
          (dividends - qualified_dividends) -
          ira_deduction -
          student_loan_interest)

income = (
    gross_wages
    + taxable_interest
    + (dividends - qualified_dividends)
    - ira_deduction
    - student_loan_interest
)

표준 라이브러리와 외부 라이브러리를 구분해 사용한다.

import os
from collections import defaultdict

import numpy as np

객체의 프로퍼티가 3개 이상이면 개행을 강제한다.

obj: Dict[str, int] = {'one': 1}
obj: Dict[str, int] = {
    'one': 1,
    'two': 2,
    'three': 3,
}
def function(a: int, b: int, c: int):
    pass
  • 데이터형 확인하기
  • 조건 확인하기

반환하기

def function() -> None:
    pass


def function() -> int:
    return 1
  • 순회하기
  • 콜백 함수의 스코프

주석

인라인(inline) 주석은 사용하지 않는다. 부득이한 경우, 두 칸 스페이스 후 작성한다.

variable_name: str = ''  # 주석

공백

함수와 함수, 클래스와 함수 등 객체들의 사이는 두 칸 공백을 준다.

def function1():
    pass


def function2():
    pass
from typing import List, Optional
from torch import nn, Tensor

class Model(nn.Module):
    def __init__(self, num_classes: int, use_amp: Optional = None):
        self.num_classes = num_classes
        self.use_amp = use_amp
    def forward(self, inputs: List[Tensor]) -> Tensor
        outputs = self.model(inputs)
        return outputs

변수에 정수

num_images: int = 10
images: List[str] = ['001.jpg', '002.jpg']

num_epochs: int = 3
step: Tuple[int] = [100, 200, 500]
info_path
image_dir
image_ids
std::string info_path
import numpy as np

box: np.ndarray[np.float64] = np.array([10.1, 20.2, 30.1, 40.2], dtype=np.float64)