KR_If - somaz94/python-study GitHub Wiki

Python ์กฐ๊ฑด๋ฌธ(If) ๊ฐœ๋… ์ •๋ฆฌ


1๏ธโƒฃ if๋ฌธ์ด๋ž€?

if๋ฌธ์€ ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ํ•˜๋Š” ์ œ์–ด๋ฌธ์ด๋‹ค. ์กฐ๊ฑด์ด ์ฐธ(True)์ผ ๋•Œ์™€ ๊ฑฐ์ง“(False)์ผ ๋•Œ ์„œ๋กœ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

# ๊ธฐ๋ณธ if๋ฌธ ๊ตฌ์กฐ
if ์กฐ๊ฑด:
    # ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ์‹คํ–‰ํ•  ์ฝ”๋“œ
    ์ˆ˜ํ–‰ํ• _๋ฌธ์žฅ1
    ์ˆ˜ํ–‰ํ• _๋ฌธ์žฅ2
else:
    # ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ ์‹คํ–‰ํ•  ์ฝ”๋“œ
    ์ˆ˜ํ–‰ํ• _๋ฌธ์žฅA
    ์ˆ˜ํ–‰ํ• _๋ฌธ์žฅB

โœ… ์ฃผ์˜์‚ฌํ•ญ:

  • ์ฝœ๋ก (:) ํ•„์ˆ˜
  • ๋“ค์—ฌ์“ฐ๊ธฐ ํ•„์ˆ˜ (๋ณดํ†ต 4์นธ ๊ณต๋ฐฑ ์‚ฌ์šฉ)
  • ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์‚ฌ์šฉ


2๏ธโƒฃ ์กฐ๊ฑด๋ฌธ ์ž‘์„ฑํ•˜๊ธฐ

# ๋น„๊ต ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ
x = 10
if x > 0:
    print("์–‘์ˆ˜์ž…๋‹ˆ๋‹ค")

# ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ
age = 25
money = 5000
if age >= 20 and money >= 10000:
    print("์ž…์žฅ ๊ฐ€๋Šฅ")

# in, not in ์—ฐ์‚ฐ์ž
fruits = ['apple', 'banana', 'orange']
if 'apple' in fruits:
    print("์‚ฌ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค")

โœ… ์ฃผ์š” ์—ฐ์‚ฐ์ž:

  • ๋น„๊ต: <, >, ==, !=, >=, <=
  • ๋…ผ๋ฆฌ: and, or, not
  • ๋ฉค๋ฒ„์‹ญ: in, not in


3๏ธโƒฃ elif ์‚ฌ์šฉํ•˜๊ธฐ

# ์—ฌ๋Ÿฌ ์กฐ๊ฑด ์ฒ˜๋ฆฌ
score = 85
if score >= 90:
    grade = 'A'
elif score >= 80:
    grade = 'B'
elif score >= 70:
    grade = 'C'
else:
    grade = 'D'

# ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ
if score >= 60:
    if score >= 90:
        grade = 'A'
    else:
        grade = 'B'
else:
    grade = 'F'

โœ… elif ํŠน์ง•:

  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์กฐ๊ฑด ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
  • if๋ฌธ ๋‹ค์Œ์—๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • else๋ฌธ ์ด์ „์— ์œ„์น˜


4๏ธโƒฃ ์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹

# ์ผ๋ฐ˜์ ์ธ if-else๋ฌธ
if score >= 60:
    message = "ํ•ฉ๊ฒฉ"
else:
    message = "๋ถˆํ•ฉ๊ฒฉ"

# ์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹
message = "ํ•ฉ๊ฒฉ" if score >= 60 else "๋ถˆํ•ฉ๊ฒฉ"

# ์ค‘์ฒฉ ์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹
grade = 'A' if score >= 90 else 'B' if score >= 80 else 'C'

โœ… ์žฅ์ :

  • ์ฝ”๋“œ ๊ฐ„๊ฒฐ์„ฑ
  • ๊ฐ€๋…์„ฑ ํ–ฅ์ƒ
  • ํ•œ ์ค„๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ


5๏ธโƒฃ pass ๋ฌธ

# ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š์„ ๋•Œ
if condition:
    pass
else:
    print("์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ ์‹คํ–‰")

# ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค ์ •์˜์‹œ ์ž„์‹œ ์ฝ”๋“œ
def my_function():
    pass

class MyClass:
    pass

โœ… pass ์‚ฌ์šฉ:

  • ์ž„์‹œ ์ฝ”๋“œ ์ž‘์„ฑ์‹œ
  • ์กฐ๊ฑด๋ฌธ์—์„œ ์‹คํ–‰ํ•  ์ฝ”๋“œ๊ฐ€ ์—†์„ ๋•Œ
  • ๊ตฌ๋ฌธ์ ์œผ๋กœ ๋ฌธ์žฅ์ด ํ•„์š”ํ•˜์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์„ ๋•Œ


6๏ธโƒฃ ์กฐ๊ฑด์‹์„ ๊ฐ’์œผ๋กœ ํ™œ์šฉํ•˜๊ธฐ

ํŒŒ์ด์ฌ์—์„œ๋Š” ์กฐ๊ฑด์‹ ์ž์ฒด๋ฅผ ๊ฐ’์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

# ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ์ •์ˆ˜๋กœ ํ™œ์šฉ
is_valid = True
count = 10 + is_valid  # 11 (True๋Š” 1๋กœ ๊ณ„์‚ฐ๋จ)

# ์กฐ๊ฑด์‹์„ ๊ณ„์‚ฐ์‹์— ํ™œ์šฉ
value = 100
is_greater_than_50 = value > 50  # True
factor = 2 if is_greater_than_50 else 0.5
result = value * factor  # 200

# ์กฐ๊ฑด์‹๊ณผ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜
numbers = [1, 2, 3, 4, 5]
evens = [n for n in numbers if n % 2 == 0]  # [2, 4]
odds = [n for n in numbers if n % 2 != 0]   # [1, 3, 5]

โœ… ํŠน์ง•:

  • True๋Š” ์ •์ˆ˜ 1, False๋Š” ์ •์ˆ˜ 0์œผ๋กœ ํ‰๊ฐ€๋จ
  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจํ„ด์— ์œ ์šฉ
  • ๋ฆฌ์ŠคํŠธ, ๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌํ—จ์…˜๊ณผ ์กฐํ•ฉ ๊ฐ€๋Šฅ


7๏ธโƒฃ match-case ๋ฌธ (Python 3.10+)

Python 3.10๋ถ€ํ„ฐ ๋„์ž…๋œ match-case ๋ฌธ์€ ๋‹ค๋ฅธ ์–ธ์–ด์˜ switch-case์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ํ›จ์”ฌ ๊ฐ•๋ ฅํ•˜๋‹ค.

# ๊ธฐ๋ณธ match-case ์‚ฌ์šฉ๋ฒ•
status = 404

match status:
    case 200:
        print("OK")
    case 404:
        print("Not Found")
    case 500:
        print("Server Error")
    case _:  # ๊ธฐ๋ณธ๊ฐ’(default)
        print("Unknown status")

# ํŒจํ„ด ๋งค์นญ ํ™œ์šฉ
def process_command(command):
    match command.split():
        case ["quit"]:
            print("์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค")
            return True
        case ["help"]:
            print("๋„์›€๋ง์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค")
        case ["add", *items]:
            print(f"์ถ”๊ฐ€ ํ•ญ๋ชฉ: {items}")
        case ["delete", item]:
            print(f"์‚ญ์ œ ํ•ญ๋ชฉ: {item}")
        case _:
            print("์•Œ ์ˆ˜ ์—†๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค")
    return False

# ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ๋งค์นญ
def process_point(point):
    match point:
        case (0, 0):
            print("์›์ ")
        case (0, y):
            print(f"Y์ถ• ์ง€์ : {y}")
        case (x, 0):
            print(f"X์ถ• ์ง€์ : {x}")
        case (x, y) if x == y:
            print(f"๋Œ€๊ฐ์„  ์ง€์ : {x}")
        case (x, y):
            print(f"์ผ๋ฐ˜ ์ง€์ : ({x}, {y})")

# ํด๋ž˜์Šค ๋งค์นญ
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

def classify_point(obj):
    match obj:
        case Point(x=0, y=0):
            print("์›์ ")
        case Point(x=0, y=y):
            print(f"Y์ถ• ์ง€์ : {y}")
        case Point(x=x, y=0):
            print(f"X์ถ• ์ง€์ : {x}")
        case Point(x=x, y=y) if x == y:
            print(f"๋Œ€๊ฐ์„  ์ง€์ : {x}")
        case Point():
            print(f"์ผ๋ฐ˜ Point: ({obj.x}, {obj.y})")
        case _:
            print("Point ๊ฐ์ฒด ์•„๋‹˜")

โœ… match-case์˜ ์žฅ์ :

  • ๋‹ค์–‘ํ•œ ํŒจํ„ด ๋งค์นญ ์ง€์›
  • ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๋‚ด์žฅ
  • ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ๊ฐ์ฒด ๋งค์นญ ๊ฐ€๋Šฅ
  • ๊ฐ€๋“œ ์ ˆ(guard clause) ์ง€์›
  • ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ ํ–ฅ์ƒ


8๏ธโƒฃ ํšจ์œจ์ ์ธ ์กฐ๊ฑด๋ฌธ ์ž‘์„ฑ ํŒจํ„ด

์กฐ๊ฑด๋ฌธ์„ ํšจ์œจ์ ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์ด๋‹ค.

1. ์กฐ๊ธฐ ๋ฐ˜ํ™˜ ํŒจํ„ด

# ์ค‘์ฒฉ if๋ฌธ ์‚ฌ์šฉ
def process_data(data):
    if data:
        if isinstance(data, list):
            if len(data) > 0:
                return sum(data)
            else:
                return 0
        else:
            return None
    else:
        return None

# ์กฐ๊ธฐ ๋ฐ˜ํ™˜์„ ์‚ฌ์šฉํ•œ ๊ฐœ์„ 
def process_data_improved(data):
    if not data:
        return None
    if not isinstance(data, list):
        return None
    if len(data) == 0:
        return 0
    return sum(data)

2. ๋””์ŠคํŒจ์น˜ ํ…Œ์ด๋ธ” ํŒจํ„ด

# if-elif ์ฒด์ธ ์‚ฌ์šฉ
def perform_operation(operation, a, b):
    if operation == 'add':
        return a + b
    elif operation == 'subtract':
        return a - b
    elif operation == 'multiply':
        return a * b
    elif operation == 'divide':
        return a / b if b != 0 else None
    else:
        return None

# ๋””์ŠคํŒจ์น˜ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•œ ๊ฐœ์„ 
def add(a, b): return a + b
def subtract(a, b): return a - b
def multiply(a, b): return a * b
def divide(a, b): return a / b if b != 0 else None

operations = {
    'add': add,
    'subtract': subtract,
    'multiply': multiply,
    'divide': divide
}

def perform_operation_improved(operation, a, b):
    if operation not in operations:
        return None
    return operations[operation](a, b)

3. ๊ฐ€๋“œ ์ ˆ(Guard Clause) ํŒจํ„ด

def calculate_discount(order_total, user_status):
    # ๋ณต์žกํ•œ ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ
    if user_status == 'premium':
        if order_total >= 100:
            discount = 0.2
        else:
            discount = 0.1
    elif user_status == 'regular':
        if order_total >= 200:
            discount = 0.15
        else:
            discount = 0.05
    else:
        discount = 0
    
    return order_total * (1 - discount)

# ๊ฐ€๋“œ ์ ˆ์„ ์‚ฌ์šฉํ•œ ๊ฐœ์„ 
def calculate_discount_improved(order_total, user_status):
    # ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •
    discount = 0
    
    # ํ”„๋ฆฌ๋ฏธ์—„ ํšŒ์› ์ฒ˜๋ฆฌ
    if user_status == 'premium':
        discount = 0.1
        if order_total >= 100:
            discount = 0.2
        return order_total * (1 - discount)
    
    # ์ผ๋ฐ˜ ํšŒ์› ์ฒ˜๋ฆฌ
    if user_status == 'regular':
        discount = 0.05
        if order_total >= 200:
            discount = 0.15
        return order_total * (1 - discount)
    
    # ๋น„ํšŒ์› ์ฒ˜๋ฆฌ
    return order_total

โœ… ํšจ์œจ์ ์ธ ์กฐ๊ฑด๋ฌธ ํŒจํ„ด ํŠน์ง•:

  • ์ค‘์ฒฉ ๊นŠ์ด๋ฅผ ์ค„์—ฌ ๊ฐ€๋…์„ฑ ํ–ฅ์ƒ
  • ์œ ์ง€๋ณด์ˆ˜ ์šฉ์ด์„ฑ ์ฆ๊ฐ€
  • ์„ฑ๋Šฅ ํ–ฅ์ƒ (๋ถˆํ•„์š”ํ•œ ์กฐ๊ฑด ๊ฒ€์‚ฌ ๋ฐฉ์ง€)
  • ์ฝ”๋“œ ์ค‘๋ณต ๊ฐ์†Œ
  • ํ™•์žฅ์„ฑ ์ฆ๊ฐ€


9๏ธโƒฃ ์กฐ๊ฑด๋ฌธ๊ณผ ์„ฑ๋Šฅ ์ตœ์ ํ™”

1. ๋‹จ๋ฝ ํ‰๊ฐ€(Short-circuit Evaluation) ํ™œ์šฉ

# ํšจ์œจ์ ์ธ ๋‹จ๋ฝ ํ‰๊ฐ€
def is_valid_user(user):
    # user๊ฐ€ None์ด๋ฉด user_id ์ ‘๊ทผ ์ „์— False ๋ฐ˜ํ™˜
    return user is not None and user.get('id') is not None

# ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กฐํšŒ๋Š” ๋น„์šฉ์ด ๋†’์€ ์ž‘์—…์ด๋ฏ€๋กœ ๋งˆ์ง€๋ง‰์— ๋ฐฐ์น˜
def can_access_resource(user, resource_id):
    if not user:
        return False
    if not resource_id:
        return False
    if not user.is_active:
        return False
    # ๋น„์šฉ์ด ๋†’์€ DB ์กฐํšŒ๋Š” ๋‹ค๋ฅธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ ํ›„์— ๋งˆ์ง€๋ง‰์œผ๋กœ ์ˆ˜ํ–‰
    return database.has_permission(user.id, resource_id)

2. ์กฐ๊ฑด ์ˆœ์„œ ์ตœ์ ํ™”

# ์กฐ๊ฑด ๊ฒ€์‚ฌ ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•œ ์ƒํ™ฉ
def process_event(event):
    # 1. ๊ฐ€์žฅ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” ์กฐ๊ฑด์„ ๋จผ์ € ๊ฒ€์‚ฌ (๋นˆ๋„ ๊ธฐ๋ฐ˜ ์ตœ์ ํ™”)
    if event.type == 'COMMON_EVENT':
        return handle_common_event(event)
    
    # 2. ๊ณ„์‚ฐ ๋น„์šฉ์ด ์ ์€ ์กฐ๊ฑด์„ ๋จผ์ € ๊ฒ€์‚ฌ (๋น„์šฉ ๊ธฐ๋ฐ˜ ์ตœ์ ํ™”)
    if not is_valid_format(event):  # ๊ฐ„๋‹จํ•œ ํ˜•์‹ ๊ฒ€์‚ฌ
        return {'error': 'Invalid format'}
    
    # 3. ๋ณต์žกํ•œ ๊ฒ€์ฆ ๋กœ์ง์€ ๋‚˜์ค‘์— ์‹คํ–‰
    if not is_authorized(event):  # ๋ณต์žกํ•œ ๊ถŒํ•œ ๊ฒ€์‚ฌ
        return {'error': 'Unauthorized'}
    
    # 4. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กฐํšŒ ๊ฐ™์€ ๋น„์šฉ์ด ๋†’์€ ์ž‘์—…์€ ๋งˆ์ง€๋ง‰์—
    return process_in_database(event)

3. ๋ชฉ์ ์— ๋งž๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ์„ ํƒ

# ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฉค๋ฒ„์‹ญ ๊ฒ€์‚ฌ (O(n) ์‹œ๊ฐ„ ๋ณต์žก๋„)
valid_statuses = ['active', 'pending', 'suspended']
if user_status in valid_statuses:
    process_user()

# ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฉค๋ฒ„์‹ญ ๊ฒ€์‚ฌ (O(1) ์‹œ๊ฐ„ ๋ณต์žก๋„)
valid_statuses = {'active', 'pending', 'suspended'}
if user_status in valid_statuses:
    process_user()

# ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ ์กฐ๊ฑด ๋ถ„๊ธฐ (O(1) ์ ‘๊ทผ)
handlers = {
    'active': handle_active_user,
    'pending': handle_pending_user,
    'suspended': handle_suspended_user
}

if user_status in handlers:
    handlers[user_status](user)

โœ… ์„ฑ๋Šฅ ์ตœ์ ํ™” ์›์น™:

  • ๊ณ„์‚ฐ ๋น„์šฉ์ด ๋‚ฎ์€ ์กฐ๊ฑด์„ ๋จผ์ € ๊ฒ€์‚ฌ
  • ๋ฐœ์ƒ ๋นˆ๋„๊ฐ€ ๋†’์€ ์กฐ๊ฑด์„ ๋จผ์ € ๊ฒ€์‚ฌ
  • ์ ์ ˆํ•œ ์ž๋ฃŒ๊ตฌ์กฐ ์„ ํƒ (๋ฆฌ์ŠคํŠธ vs ์„ธํŠธ vs ๋”•์…”๋„ˆ๋ฆฌ)
  • ๋‹จ๋ฝ ํ‰๊ฐ€๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ์—ฐ์‚ฐ ๋ฐฉ์ง€
  • ์กฐ๊ฑด๋ฌธ๋ณด๋‹ค ๋””์ŠคํŒจ์น˜ ํ…Œ์ด๋ธ”์ด ํšจ์œจ์ ์ธ ๊ฒฝ์šฐ ๊ณ ๋ ค


๐Ÿ”Ÿ ์‹ค์ „ ์‘์šฉ ํŒจํ„ด

1. ์„ค์ • ๊ฐ’ ํด๋ฐฑ ์ฒด์ธ

# ์„ค์ • ๊ฐ’์„ ์ฝ๋Š” ์˜ˆ์‹œ (ํ™˜๊ฒฝ๋ณ€์ˆ˜ โ†’ ์„ค์ • ํŒŒ์ผ โ†’ ๊ธฐ๋ณธ๊ฐ’)
def get_config(key, default=None):
    # ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ๋จผ์ € ํ™•์ธ
    env_var = os.environ.get(f'APP_{key.upper()}')
    if env_var is not None:
        return env_var
    
    # ์„ค์ • ํŒŒ์ผ์—์„œ ํ™•์ธ
    if config and key in config:
        return config[key]
    
    # ๊ธฐ๋ณธ๊ฐ’ ์‚ฌ์šฉ
    return default

2. ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์™€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

def calculate_investment_return(principal, rate, years):
    # ์ž…๋ ฅ๊ฐ’ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
    if not isinstance(principal, (int, float)) or principal <= 0:
        raise ValueError("์›๊ธˆ์€ ์–‘์ˆ˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค")
    
    if not isinstance(rate, (int, float)) or rate < 0:
        raise ValueError("์ด์œจ์€ 0 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค")
    
    if not isinstance(years, (int, float)) or years <= 0:
        raise ValueError("๊ธฐ๊ฐ„์€ ์–‘์ˆ˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค")
    
    # ๊ณ„์‚ฐ ๋กœ์ง
    return principal * ((1 + rate/100) ** years)

3. ์ƒํƒœ ๊ธฐ๊ณ„(State Machine) ๊ตฌํ˜„

class OrderState:
    CREATED = 'created'
    PAID = 'paid'
    SHIPPED = 'shipped'
    DELIVERED = 'delivered'
    CANCELED = 'canceled'

class Order:
    def __init__(self):
        self.state = OrderState.CREATED
    
    def pay(self):
        if self.state == OrderState.CREATED:
            self.state = OrderState.PAID
            return True
        return False
    
    def ship(self):
        if self.state == OrderState.PAID:
            self.state = OrderState.SHIPPED
            return True
        return False
    
    def deliver(self):
        if self.state == OrderState.SHIPPED:
            self.state = OrderState.DELIVERED
            return True
        return False
    
    def cancel(self):
        if self.state in (OrderState.CREATED, OrderState.PAID):
            self.state = OrderState.CANCELED
            return True
        return False

4. ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž์™€ ์กฐ๊ฑด๋ถ€ ์‹คํ–‰

from contextlib import contextmanager

@contextmanager
def conditionally_log(condition, logger):
    """์กฐ๊ฑด๋ถ€ ๋กœ๊น…์„ ์œ„ํ•œ ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž"""
    if condition:
        logger.info("์ž‘์—… ์‹œ์ž‘")
        try:
            yield
            logger.info("์ž‘์—… ์„ฑ๊ณต ์™„๋ฃŒ")
        except Exception as e:
            logger.error(f"์ž‘์—… ์‹คํŒจ: {e}")
            raise
    else:
        yield  # ๋กœ๊น… ์—†์ด ์‹คํ–‰

# ์‚ฌ์šฉ ์˜ˆ์‹œ
import logging
logger = logging.getLogger('app')

debug_mode = True
with conditionally_log(debug_mode, logger):
    # ์ž‘์—… ์ˆ˜ํ–‰
    result = process_data()

โœ… ์‹ค์ „ ํŒจํ„ด ์žฅ์ :

  • ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ฑ ์ฆ๊ฐ€
  • ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ๊ฐœ์„ 
  • ์œ ์ง€๋ณด์ˆ˜ ์šฉ์ด์„ฑ ํ–ฅ์ƒ
  • ๊ฐ€๋…์„ฑ ๋ฐ ํ™•์žฅ์„ฑ ์ฆ๊ฐ€
  • ์ƒํ™ฉ๋ณ„ ์ ์ ˆํ•œ ๋Œ€์‘ ๊ฐ€๋Šฅ


โš ๏ธ **GitHub.com Fallback** โš ๏ธ