λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ’» Programming/Python

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

by 뭅즀 2024. 7. 11.
λ°˜μ‘ν˜•

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


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)

 


 

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

λ°˜μ‘ν˜•