[python] μ€‘κ³ κΈ‰μžλ₯Ό μœ„ν•œ 파이썬 클린 μ½”λ“œ μž‘μ„± κΏ€νŒ 8κ°€μ§€ : 더 κΉ”λ”ν•˜κ³  μœ μ§€λ³΄μˆ˜ μ‰¬μš΄ μ½”λ“œ μž‘μ„±ν•˜κΈ°!

2024. 7. 11. 20:58Β·πŸ’» Programming/Python
λ°˜μ‘ν˜•

μ˜€λŠ˜μ€ 파이썬 클린 μ½”λ“œ μž‘μ„±μ— λŒ€ν•œ μ€‘κ³ κΈ‰μž λ²„μ „μ˜ νŒμ„ μ†Œκ°œν•΄λ“œλ¦΄κ²Œμš”. μ΄ˆκΈ‰μž νŒλ³΄λ‹€ 더 μ‹¬ν™”λœ λ‚΄μš©μ„ 닀루며, μ—¬λŸ¬λΆ„μ˜ μ½”λ“œ ν’ˆμ§ˆμ„ ν•œ 단계 μ—…κ·Έλ ˆμ΄λ“œν•  수 μžˆλŠ” 방법듀을 μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€. 😊


1. ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ²˜ κ°œμ„ ν•˜κΈ°

ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜μ™€ λ°˜ν™˜ νƒ€μž…μ„ λͺ…μ‹œν•˜λ©΄ μ½”λ“œμ˜ 가독성과 μœ μ§€λ³΄μˆ˜μ„±μ΄ 크게 ν–₯μƒλ©λ‹ˆλ‹€. 이λ₯Ό μœ„ν•΄ νƒ€μž… 힌트λ₯Ό μ‚¬μš©ν•΄λ³΄μ„Έμš”.

# λ‚˜μœ 예
def calculate_area(length, width):
    return length * width

# 쒋은 예
def calculate_area(length: float, width: float) -> float:
    return length * width

 

2. μ»¨ν…μŠ€νŠΈ λ§€λ‹ˆμ € μ‚¬μš©ν•˜κΈ°

λ¦¬μ†ŒμŠ€λ₯Ό 관리할 λ•Œ μ»¨ν…μŠ€νŠΈ λ§€λ‹ˆμ €λ₯Ό μ‚¬μš©ν•˜λ©΄ 더 μ•ˆμ „ν•˜κ³  κ°„κ²°ν•œ μ½”λ“œλ₯Ό μž‘μ„±ν•  수 μžˆμ–΄μš”. 예λ₯Ό λ“€μ–΄ 파일 μž…μΆœλ ₯μ΄λ‚˜ λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²°μ—μ„œ μœ μš©ν•©λ‹ˆλ‹€.

# λ‚˜μœ 예
file = open('example.txt', 'r')
data = file.read()
file.close()

# 쒋은 예
with open('example.txt', 'r') as file:
    data = file.read()

 

 

3. λͺ¨λ“ˆν™”와 νŒ¨ν‚€μ§€ν™”

μ½”λ“œλ₯Ό λͺ¨λ“ˆν™”ν•˜κ³  νŒ¨ν‚€μ§€ν™”ν•˜λ©΄ μž¬μ‚¬μš©μ„±κ³Ό μœ μ§€λ³΄μˆ˜μ„±μ΄ λ†’μ•„μ§‘λ‹ˆλ‹€. κΈ°λŠ₯λ³„λ‘œ λͺ¨λ“ˆμ„ λΆ„λ¦¬ν•˜κ³ , κ΄€λ ¨ λͺ¨λ“ˆλ“€μ„ νŒ¨ν‚€μ§€λ‘œ λ¬Άμ–΄λ³΄μ„Έμš”.

# λͺ¨λ“ˆν™” μ „
def fetch_data():
    pass

def process_data():
    pass

# λͺ¨λ“ˆν™” ν›„
# data_fetcher.py
def fetch_data():
    pass

# data_processor.py
def process_data():
    pass

 

 

4. 좔상화와 μΈν„°νŽ˜μ΄μŠ€

좔상화λ₯Ό 톡해 μ½”λ“œμ˜ λ³΅μž‘μ„±μ„ 쀄이고, μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•΄ κ΅¬ν˜„μ²΄λ₯Ό κ΅μ²΄ν•˜κΈ° μ‰½κ²Œ λ§Œλ“€ 수 μžˆμ–΄μš”.

# λ‚˜μœ 예
class FileStorage:
    def store(self, data):
        # 파일 μ €μž₯ μ½”λ“œ

class DataProcessor:
    def __init__(self, storage: FileStorage):
        self.storage = storage

# 쒋은 예
from abc import ABC, abstractmethod

class Storage(ABC):
    @abstractmethod
    def store(self, data):
        pass

class FileStorage(Storage):
    def store(self, data):
        # 파일 μ €μž₯ μ½”λ“œ

class DataProcessor:
    def __init__(self, storage: Storage):
        self.storage = storage

 

 

5. λΆˆλ³€ 객체 μ‚¬μš©ν•˜κΈ°

λΆˆλ³€ 객체λ₯Ό μ‚¬μš©ν•˜λ©΄ μƒνƒœ λ³€κ²½μœΌλ‘œ μΈν•œ 버그λ₯Ό 쀄일 수 μžˆμ–΄μš”. νŒŒμ΄μ¬μ—μ„œλŠ” namedtupleμ΄λ‚˜ dataclass(frozen=True)λ₯Ό μ‚¬μš©ν•΄ λΆˆλ³€ 객체λ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

# λ‚˜μœ 예
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

point = Point(1, 2)
point.x = 3  # μƒνƒœ λ³€κ²½ κ°€λŠ₯

# 쒋은 예
from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])
point = Point(1, 2)
# point.x = 3  # μƒνƒœ λ³€κ²½ λΆˆκ°€λŠ₯

 

 

6. 디버깅과 λ‘œκΉ…

효율적인 디버깅과 λ‘œκΉ…μ€ μ½”λ“œμ˜ μ•ˆμ •μ„±μ„ λ†’μ—¬μ€λ‹ˆλ‹€. logging λͺ¨λ“ˆμ„ μ‚¬μš©ν•΄ λ‘œκΉ…μ„ μ„€μ •ν•˜κ³ , 디버깅 μ‹œμ—λŠ” pdbλ₯Ό ν™œμš©ν•΄λ³΄μ„Έμš”.

import logging

logging.basicConfig(level=logging.INFO)

def divide(a, b):
    logging.info(f"Dividing {a} by {b}")
    if b == 0:
        logging.error("Division by zero!")
        return None
    return a / b

result = divide(10, 2)

 

 

7. ν…ŒμŠ€νŠΈ 컀버리지 높이기

ν…ŒμŠ€νŠΈ 컀버리지λ₯Ό λ†’μ—¬ μ½”λ“œμ˜ μ•ˆμ •μ„±μ„ 보μž₯ν•˜μ„Έμš”. λ‹¨μœ„ ν…ŒμŠ€νŠΈ, 톡합 ν…ŒμŠ€νŠΈλ₯Ό μž‘μ„±ν•˜κ³ , pytest와 같은 도ꡬλ₯Ό μ‚¬μš©ν•΄ ν…ŒμŠ€νŠΈ 컀버리지λ₯Ό μΈ‘μ •ν•΄λ³΄μ„Έμš”.

def add(a, b):
    return a + b

def test_add():
    assert add(1, 2) == 3
    assert add(-1, 1) == 0

if __name__ == "__main__":
    test_add()
    print("All tests passed!")

 

 

8. μ½”λ“œ λ¦¬νŒ©ν† λ§

μ •κΈ°μ μœΌλ‘œ μ½”λ“œλ₯Ό λ¦¬νŒ©ν† λ§ν•΄ 가독성과 μœ μ§€λ³΄μˆ˜μ„±μ„ λ†’μ΄μ„Έμš”. λΆˆν•„μš”ν•œ 쀑볡을 μ œκ±°ν•˜κ³ , μ½”λ“œλ₯Ό 더 κ°„κ²°ν•˜κ²Œ λ§Œλ“œλŠ” 것이 μ€‘μš”ν•΄μš”.

# λ¦¬νŒ©ν† λ§ μ „
def get_user_name(user):
    return user['name']

def get_user_email(user):
    return user['email']

# λ¦¬νŒ©ν† λ§ ν›„
def get_user_attribute(user, attribute):
    return user.get(attribute)

 


 

μ΄λŸ¬ν•œ μ€‘κ³ κΈ‰μž νŒλ“€μ„ ν™œμš©ν•˜λ©΄ 파이썬 μ½”λ“œμ˜ ν’ˆμ§ˆμ΄ ν•œμΈ΅ 더 ν–₯상될 κ±°μ˜ˆμš”. 클린 μ½”λ“œλ₯Ό μž‘μ„±ν•˜λŠ” μŠ΅κ΄€μ„ 듀이면, μ½”λ“œ μœ μ§€λ³΄μˆ˜μ™€ ν˜‘μ—…μ΄ 훨씬 더 μˆ˜μ›”ν•΄μ§ˆ κ²λ‹ˆλ‹€. 😊

λ°˜μ‘ν˜•

'πŸ’» Programming > Python' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[python] ν˜„μž¬ 파일의 디렉토리 경둜λ₯Ό μ†μ‰½κ²Œ μ°ΎκΈ°: os.path λŒ€μ‹  pathlib μ‚¬μš©ν•˜κΈ°  (0) 2024.07.30
[python] Streamlit κΈ°λ³Έ κΈ°λŠ₯ 읡히기 ! | ν…μŠ€νŠΈ & 데이터 ν‘œμ‹œ | λ ˆμ΄μ•„μ›ƒ 관리 | 파일 μ—…λ‘œλ“œ & λ‹€μš΄λ‘œλ“œ  (0) 2024.07.14
[python] 파이썬 클린 μ½”λ“œ μž‘μ„± κΏ€νŒ 8κ°€μ§€ : 더 κΉ”λ”ν•˜κ³  가독성 높은 μ½”λ“œ μž‘μ„±ν•˜κΈ°!  (0) 2024.07.11
[python] Streamlit 으둜 데이터 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ§Œλ“€κΈ°! | κ°„λ‹¨ν•œ λŒ€μ‹œλ³΄λ“œ & 웹데λͺ¨ νŽ˜μ΄μ§€ 개발  (1) 2024.07.08
[python] 파이썬 병렬 처리 | joblib μ‚¬μš©λ²• | λ©€ν‹°ν”„λ‘œμ„Έμ‹± | λ©€ν‹°μ“°λ ˆλ”©  (0) 2024.01.19
'πŸ’» Programming/Python' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • [python] ν˜„μž¬ 파일의 디렉토리 경둜λ₯Ό μ†μ‰½κ²Œ μ°ΎκΈ°: os.path λŒ€μ‹  pathlib μ‚¬μš©ν•˜κΈ°
  • [python] Streamlit κΈ°λ³Έ κΈ°λŠ₯ 읡히기 ! | ν…μŠ€νŠΈ & 데이터 ν‘œμ‹œ | λ ˆμ΄μ•„μ›ƒ 관리 | 파일 μ—…λ‘œλ“œ & λ‹€μš΄λ‘œλ“œ
  • [python] 파이썬 클린 μ½”λ“œ μž‘μ„± κΏ€νŒ 8κ°€μ§€ : 더 κΉ”λ”ν•˜κ³  가독성 높은 μ½”λ“œ μž‘μ„±ν•˜κΈ°!
  • [python] Streamlit 으둜 데이터 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ§Œλ“€κΈ°! | κ°„λ‹¨ν•œ λŒ€μ‹œλ³΄λ“œ & 웹데λͺ¨ νŽ˜μ΄μ§€ 개발
뭅즀
뭅즀
AI 기술 λΈ”λ‘œκ·Έ
    λ°˜μ‘ν˜•
  • 뭅즀
    CV DOODLE
    뭅즀
  • 전체
    였늘
    μ–΄μ œ
  • 곡지사항

    • ✨ About Me
    • λΆ„λ₯˜ 전체보기 (198)
      • πŸ“– Fundamentals (33)
        • Computer Vision (9)
        • 3D vision & Graphics (6)
        • AI & ML (15)
        • NLP (2)
        • etc. (1)
      • πŸ› Research (64)
        • Deep Learning (7)
        • Image Classification (2)
        • Detection & Segmentation (17)
        • OCR (7)
        • Multi-modal (4)
        • Generative AI (6)
        • 3D Vision (2)
        • Material & Texture Recognit.. (8)
        • NLP & LLM (11)
        • etc. (0)
      • 🌟 AI & ML Tech (7)
        • AI & ML μΈμ‚¬μ΄νŠΈ (7)
      • πŸ’» Programming (85)
        • Python (18)
        • Computer Vision (12)
        • LLM (4)
        • AI & ML (17)
        • Database (3)
        • Apache Airflow (6)
        • Docker & Kubernetes (14)
        • μ½”λ”© ν…ŒμŠ€νŠΈ (4)
        • C++ (1)
        • etc. (6)
      • πŸ’¬ ETC (3)
        • μ±… 리뷰 (3)
  • 링크

  • 인기 κΈ€

  • νƒœκ·Έ

    airflow
    LLM
    OCR
    Computer Vision
    VLP
    λ”₯λŸ¬λ‹
    CNN
    pytorch
    Text recognition
    material recognition
    Python
    nlp
    pandas
    객체 κ²€μΆœ
    AI
    OpenCV
    도컀
    object detection
    GPT
    Image Classification
    ChatGPT
    deep learning
    3D Vision
    segmentation
    multi-modal
    ν”„λ‘¬ν”„νŠΈμ—”μ§€λ‹ˆμ–΄λ§
    파이썬
    OpenAI
    κ°μ²΄κ²€μΆœ
    컴퓨터비전
  • 졜근 λŒ“κΈ€

  • 졜근 κΈ€

  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.3
뭅즀
[python] μ€‘κ³ κΈ‰μžλ₯Ό μœ„ν•œ 파이썬 클린 μ½”λ“œ μž‘μ„± κΏ€νŒ 8κ°€μ§€ : 더 κΉ”λ”ν•˜κ³  μœ μ§€λ³΄μˆ˜ μ‰¬μš΄ μ½”λ“œ μž‘μ„±ν•˜κΈ°!
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”