진행 중인 프로젝트에서 코드를 Python으로 여러 사람이 같이 짜야 하는 상황이 발생했다.
여러 사람이 같이 진행하다 보니까 코딩 스타일도 다르고
함수명이랑 클래스명등 명명 규칙이 꼬이고 주석도 다양한 스타일로 달다 보니 좀 혼돈 스러운 경향이 있었다.
나 같은 경우도 Java 개발 때의 습관이 있어서
코드 스타일을 Java로 하고 주석도 java 스타일로 달고 있었다.
그런데 갑자기 굼금해 졌다.
Python은 어떤 코드 스타일을 추구 하고 있는가? 라는??
찾아 보니 여러가지 스타일 들이있다.
1.PEP8
https://www.python.org/dev/peps/pep-0008/
파이썬 창시자 Guido van Rossum이 작성한 파이썬 스타일 가이드이다.
창시자가 만들었으면 한번쯤은 다 읽어 봐야 할 작성 법이다.
다 읽어 보고 싶으나 그럴 상황이 아니기에 다른 사람들이 요약한 요점만 정리 하자면
module이름은 only 소문자
class이름은 CamelCase
function이름은 snake_case
들여쓰기는 4칸
2.Google Python Style guide
https://google.github.io/styleguide/pyguide.html
PEP8과 유사하다고 한다.
Tensorflow 코드를 보면 많이 접하게 된다고 한다. 난 무심하게 넘겼다..ㅡㅡ
차이점은 주석 적는 법 "Docstring"을 작성하는 법에 대해서 깊이 다루고 있다고 한다.
사이트에서 samplecode를 가져 와서 보면
Yes:
def connect_to_next_port(self, minimum):
"""Connects to the next available port.
Args:
minimum: A port value greater or equal to 1024.
Raises:
ValueError: If the minimum port specified is less than 1024.
ConnectionError: If no available port is found.
Returns:
The new minimum port.
"""
if minimum < 1024:
raise ValueError('Minimum port must be at least 1024, not %d.' % (minimum,))
port = self._find_next_open_port(minimum)
if not port:
raise ConnectionError('Could not connect to service on %d or higher.' % (minimum,))
assert port >= minimum, 'Unexpected port %d when minimum was %d.' % (port, minimum)
return port
No:
def connect_to_next_port(self, minimum):
"""Connects to the next available port.
Args:
minimum: A port value greater or equal to 1024.
Returns:
The new minimum port.
"""
assert minimum >= 1024, 'Minimum port must be at least 1024.'
port = self._find_next_open_port(minimum)
assert port is not None
return port
이렇게 잘 나와 있다.
Yes: import math
class Square(object):
"""A square with two properties: a writable area and a read-only perimeter.
To use:
>>> sq = Square(3)
>>> sq.area
9
>>> sq.perimeter
12
>>> sq.area = 16
>>> sq.side
4
>>> sq.perimeter
16
"""
def __init__(self, side):
self.side = side
@property
def area(self):
"""Gets or sets the area of the square."""
return self._get_area()
@area.setter
def area(self, area):
return self._set_area(area)
def _get_area(self):
"""Indirect accessor to calculate the 'area' property."""
return self.side ** 2
def _set_area(self, area):
"""Indirect setter to set the 'area' property."""
self.side = math.sqrt(area)
@property
def perimeter(self):
return self.side * 4
3.Numpy/Scipy Style Guide
https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
numpy에서 사용되는 것인데 이건 참고 사이트 만 걸겠다.
'프로그래밍 > python' 카테고리의 다른 글
pyenv를 이용한 여러 버전의 파이선 하나의 컴퓨터에서 이용하 (1) | 2024.11.08 |
---|---|
Python argparse.ArgumentParser() (0) | 2023.04.24 |
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (0) | 2019.02.25 |
pyinstaller를 이용한 tensorflow-gpu exe 파일 만들기 (5) | 2018.11.09 |
Python DataFrame To excel with hyperlink (0) | 2018.09.27 |