Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Image Generation
- controlNet
- 포스코 채용
- diffusion models
- stable diffusion
- colorization
- DDPM
- manganinja
- 코딩테스트
- classifier-free guidance
- Generative Models
- KT
- ip-adapter
- 포스코 코딩테스트
- kt인적성
- 프로그래머스
- 과제형 코딩테스트
- 논문 리뷰
- ddim
- posco 채용
- dp
Archives
- Today
- Total
Paul's Grit
[Python] PEP8 - Style Guide for Python Code 에 대하여 본문
PEP8
- Python Ehancement Proposal 8
- 파이썬 코드 작성 규칙(컨벤션)(제안사항)
- [문서] https://peps.python.org/pep-0008/
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
가로 간격
- 들여쓰기(indentation)
- 공백 4칸
- 2칸은 구분이 잘 아돼서
- 8칸은 간격이 너무 넓음
- 2, 4, 8, 16과 같은 2의 거듭제곱 수를 선호하는 경향이 있었음
for i in range(3):
print(i)
Cell In[4], line 2
print(i)
^
IndentationError: expected an indented block after 'for' statement on line 1
# 띄어 쓰기 2번이어도 실행은 된다
for i in range(3):
print(i)
0
1
2
세로 간격
# 나쁜 예
class MyCalc:
def __init__():
pass
def plus():
pass
def minus():
pass
# 권장하는 예
class MyCalc():
def __init__():
pass
def plus():
pass
def minus():
pass
연산자와 식별자 사이에 공백 한 칸
# 나쁜 예
blank=blank[1]+blank[2]+blank[3]
# 좋은 예
blank = blank[1] + blank[2] + blank[3]
구분자는 뒤쪽에 공백 한 칸
# 나쁜 예
def spam(eggs, bacon, ham):
weights = [42.0,3.1415,2.718]
# 좋은 예
def spam(eggs, bacon, ham):
weights = [42.0, 3.1415, 2.718]
마침표 앞뒤에는 공백을 넣지 않는다
import random
# 나쁜 예
random . randint()
# 좋은 예
random.choice()
함수, 메서드 등 이름 뒤에는 공백을 넣지 않는다
# 나쁜 예
print ("Hello, World!")
# 좋은 예
print("Hello, World!")
data = [1, 2, 3]
# 나쁜 예
data [2]
좋은 예
data[2]
코드해애 끝의 주석 앞에는 공백 2칸
# 좋은 예
import random
random.randint(1, 10) # 1 부터 10 중 랜덤하게 숫자 하나를 뽑습니다
# 나쁜 예
import random
random.randint(1, 10) # 1 부터 10 중 랜덤하게 숫자 하나를 뽑습니다
random.randint(1, 10)# 1 부터 10 중 랜덤하게 숫자 하나를 뽑습니다
모듈 불러오기 컨벤션
# 나쁜 예
import random, sys, os
# 좋은 예
import random
import sys
import os
# 나쁜 예
from urllib.request import Request
import pandas as pd
import numpy as np
# 좋은 예: import문을 먼저 쓴다
import pandas as pd
import numpy as np
from urllib import Request
작명 규칙
- 스네이크 케이스
- 변수, 함수, 메서드, 이름은 소문자로
- 카멜 케이스(파스칼 케이스)
- 클래스 이름은 대문자로
- camelCase(카멜케이스)
- CamelCase(파스칼케이스)
# 나쁜 예
MyDatas = [7, 8, 9]
# 좋은 예
my_list = [1, 2, 3]
my_data = [4, 5, 6]
# 나쁜 예
def FuncCalc():
pass
# 좋은 예
def func_calc():
pass
# 나쁜 예
class my_calc():
pass
# 좋은 예
class MyClass():
pass
'Data Analysis > Python' 카테고리의 다른 글
[Python] Stack / Queue 구현 (0) | 2024.03.28 |
---|---|
[Python] 반복문 & List comprehension (1) | 2023.08.03 |
[Python] 조건문 & 문자열 포매팅 (0) | 2023.08.03 |
[Python] 파이썬 문법 기초 (1) | 2023.08.02 |