ã¯ããã«ïŒprint()ãããã°ããã®åæ¥ð
Pythonã§ã³ãŒããæžããŠãããšããããããã®å€æ°ã®å€ãªãã ã£ãïŒãããã®é¢æ°ãã¡ãããšæåŸ éãåããŠãïŒããšçåã«æãç¬éããããŸãããïŒ ð€ ãããªæãå€ãã®éçºè ãé Œãã®ã `print()` é¢æ°ã§ããå€æ°ã®å€ãã¿ãŒããã«ã«åºåããŠãã³ãŒãã®åããè¿œãããããããprintãããã°ãã¯ãæ軜ã§çŽæçãªæ¹æ³ã§ãã
ãããããããžã§ã¯ããè€éã«ãªã£ããããããã°ç®æãå¢ããããããšã`print()` ã ãã§ã¯éçãèŠããŠããŸãã
- åºåã®æŽªæ°Žð: 倧éã® `print()` åºåã®äžãããç®çã®æ å ±ãèŠã€ãåºãã®ã倧å€ã
- æäœæ¥ã®æéð«: `print(f”å€æ°xã®å€: {x}”)` ã®ããã«ãå€æ°åãšå€ãã»ããã§è¡šç€ºãããã®ãé¢åã
- åé€å¿ãðïž: ãããã°ãçµãã£ãåŸã`print()` æãæ¶ãå¿ããŠãæ¬çªç°å¢ã§æå³ããªãåºåãããŠããŸãããšãâŠã
ããã§ç»å Žããã®ããä»åã玹ä»ãã `icecream` ã©ã€ãã©ãªã§ãïŒðš `icecream` ã¯ã`print()` ãããã°ã®ç ©ãããã解æ¶ããããã¹ããŒãã§å¹ççãªãããã°äœéšãæäŸããŠãããããŸãã«ããããã°ã®æäžäž»ããšãèšããã©ã€ãã©ãªãªã®ã§ãã
ãã®èšäºã§ã¯ã`icecream` ã®åºæ¬çãªäœ¿ãæ¹ããã䟿å©ãªå¿çšãã¯ããã¯ãã«ã¹ã¿ãã€ãºæ¹æ³ãŸã§ã培åºçã«è§£èª¬ããŠãããŸãããããèªãã°ãããªãã `print()` ãããã°ããåæ¥ãã`icecream` ã䜿ã£ãå¿«é©ãªãããã°ã©ã€ããéããããã«ãªãã¯ãã§ãïŒâš
IceCreamãšã¯ïŒ ð€ – print()ãè¶ ãããããã°äœéš
`icecream` ã¯ãPythonã®ãããã°ãããç°¡åã§ãèŠãããããããŠïŒå°ãã ãïŒæ¥œããããããã®ãµãŒãããŒãã£ã©ã€ãã©ãªã§ããéçºè 㯠grunsæ° ã§ãGitHubãªããžã㪠(https://github.com/gruns/icecream) ã¯å€ãã®ã¹ã¿ãŒãéããŠãããPythonã³ãã¥ããã£ã§åºãåãå ¥ããããŠããŸãã
`icecream` ã®æ倧ã®ç¹åŸŽã¯ã`print()` é¢æ°ã®ä»£æ¿ãšããŠäœ¿ãã `ic()` é¢æ°ãæäŸããŠããç¹ã§ãã`ic()` 㯠`print()` ãšäŒŒãæèŠã§äœ¿ããŸããã以äžã®ãããªåŒ·åãªã¡ãªããããããŸãã
- å€æ°åãšå€ãèªå衚瀺: `ic(my_variable)` ãšæžãã ãã§ã`ic| my_variable: [å€]` ã®ããã«ãå€æ°åãšãã®å€ãèªåã§è¡šç€ºããŠãããŸãããã `print(f”my_variable: {my_variable}”)` ãšæžãå¿ èŠã¯ãããŸããïŒ
- å®è¡ç®æã®ã³ã³ããã¹ã衚瀺: åŒæ°ãªã㧠`ic()` ãåŒã³åºããšãå®è¡ããããã¡ã€ã«åãè¡çªå·ãé¢æ°åãæå»ãªã©ã衚瀺ããŠãããŸããã³ãŒãã®ã©ããå®è¡ãããããæ£ç¢ºã«ææ¡ã§ããŸãã
- èŠãããæŽåœ¢åºå: ãªã¹ããèŸæžãªã©ã®ããŒã¿æ§é ããã€ã³ãã³ããã€ããŠèŠãããæŽåœ¢ïŒpretty-printïŒããŠãããŸããè€éãªããŒã¿ãäžç®ã§ç解ãããããªããŸãã
- ã·ã³ã¿ãã¯ã¹ãã€ã©ã€ã: åºåã«è²ãä»ããã³ãŒãã®ããã«èŠããããªããŸãïŒã¿ãŒããã«ã®èšå®ã«ãããŸãïŒã
- ç°¡åãªæå¹å/ç¡å¹å: ã³ãŒãã®å é 㧠`ic.disable()` ãåŒã³åºãã ãã§ããããžã§ã¯ãå šäœã® `ic()` åºåããŸãšããŠãªãã«ã§ããŸãããããã°çµäºåŸã® `print()` åé€ã®æéãçããŸãã
- ã¿ã€ãã³ã°éãåæž: `print` ããã¿ã€ãæ°ãå°ãªã `ic` ã§æžã¿ãŸããïŒéçºè æ°ãã60%éããªãããã§ãïŒ ðïŒ
ãã®ããã«ã`icecream` 㯠`print()` ãããã°ã®æéãå€§å¹ ã«åæžããããå€ãã®æ å ±ãæäŸããŠãããããããããã°äœæ¥ã®å¹çãæ Œæ®µã«åäžãããããšãã§ããã®ã§ãã
ã€ã³ã¹ããŒã«ãšåºæ¬çãªäœ¿ãæ¹ ð ïž
ã€ã³ã¹ããŒã«
`icecream` ã¯æšæºã©ã€ãã©ãªã§ã¯ãªããããpipã䜿ã£ãŠã€ã³ã¹ããŒã«ããŸããã¿ãŒããã«ã§ä»¥äžã®ã³ãã³ããå®è¡ããŠãã ããã
pip install icecream
(2021幎2ææç¹ã®æ å ±ã§ã¯ãcondaã§ã®ã€ã³ã¹ããŒã«ã¯å ¬åŒã«ã¯ãµããŒããããŠããªãã£ãããã§ãããpipçµç±ã§ã€ã³ã¹ããŒã«ããã®ãäžè¬çã§ãã)
åºæ¬çãªäœ¿ãæ¹ïŒic() é¢æ°
ã€ã³ã¹ããŒã«ãå®äºããããPythonã¹ã¯ãªãã㧠`ic` ãã€ã³ããŒãããŠäœ¿ããŸãã
from icecream import ic
# å€æ°ã®å€ã確èªãã
name = "Alice"
age = 30
scores = [85, 92, 78]
user_info = {"id": 1, "status": "active"}
ic(name)
ic(age)
ic(scores)
ic(user_info)
# åŒã®çµæã確èªãã
x = 10
y = 5
ic(x * y)
ic(x > y)
# è€æ°ã®å€ãäžåºŠã«ç¢ºèªãã
ic(name, age, user_info["status"])
ãããå®è¡ãããšãã¿ãŒããã«ã«ã¯ä»¥äžã®ããã«è¡šç€ºãããŸãïŒè¡šç€ºåœ¢åŒã¯ç°å¢ã«ãã£ãŠå€å°ç°ãªãå ŽåããããŸãïŒã
ic| name: 'Alice'
ic| age: 30
ic| scores: [85, 92, 78]
ic| user_info: {'id': 1, 'status': 'active'}
ic| x * y: 50
ic| x > y: True
ic| name: 'Alice', age: 30, user_info["status"]: 'active'
ã©ãã§ããããïŒ `print()` ããããã£ãšæ å ±éãå€ããäœã®å€ã衚瀺ãããŠãããäžç®çç¶ã§ãããïŒâš ããã `icecream` ã®åºæ¬çãªåšåã§ãã
åŒæ°ãªãã§ã®å®è¡ïŒå®è¡ç®æã®ç¹å®
`ic()` ãåŒæ°ãªãã§åŒã³åºããšãã³ãŒãã®ã©ã®éšåãå®è¡ãããããç¥ãããšãã§ããŸããããã¯ãç¹å®ã®æ¡ä»¶åå²ãã«ãŒããå®è¡ããããã©ããã確èªããã®ã«äŸ¿å©ã§ãã
from icecream import ic
import time # æå»è¡šç€ºã®ãã
def process_data(data):
ic() # é¢æ°ãåŒã³åºãããããšã確èª
if not data:
ic() # ããŒã¿ã空ã®å Žåã®åŠçã«å
¥ã£ãããšã確èª
return "No data"
else:
ic() # ããŒã¿ãããå Žåã®åŠçã«å
¥ã£ãããšã確èª
# äœããã®åŠç...
result = f"Processed {len(data)} items"
ic(result) # åŠççµæã確èª
return result
process_data([])
print("-" * 20)
process_data([1, 2, 3])
å®è¡çµæïŒæå»ã¯å®è¡ç°å¢ã«ããç°ãªããŸãïŒïŒ
ic| script.py:5 in process_data() at 07:56:00.123
ic| script.py:7 in process_data() at 07:56:00.123
--------------------
ic| script.py:5 in process_data() at 07:56:00.123
ic| script.py:10 in process_data() at 07:56:00.123
ic| result: 'Processed 3 items'
ãã¡ã€ã«å (`script.py`)ãè¡çªå·ãé¢æ°å (`process_data`)ããããŠå®è¡æå»ã衚瀺ãããããã°ã©ã ã®å®è¡ãããŒãæ£ç¢ºã«è¿œè·¡ã§ããŸããâ°
å¿çšãã¯ããã¯ïŒãã£ãšäŸ¿å©ã«ïŒð
`icecream` ã®é åã¯åºæ¬çãªäœ¿ãæ¹ã ãã§ã¯ãããŸãããæ§ã ãªå¿çšãã¯ããã¯ã§ãããã«ãããã°ãå¹çåã§ããŸãã
é¢æ°å ã§ã®æŽ»çš
é¢æ°ã®åŒæ°ãæ»ãå€ãå éšã®å€æ°ã®å€åã远跡ããã®ã« `ic()` ã¯æé©ã§ãã
from icecream import ic
class BankAccount:
def __init__(self, balance=0):
self.balance = ic(balance) # åææ®é«ã確èª
def deposit(self, amount):
ic(amount) # å
¥éé¡ã確èª
if amount <= 0:
ic("Deposit amount must be positive")
return
self.balance += amount
ic(self.balance) # æŽæ°åŸã®æ®é«ã確èª
def withdraw(self, amount):
ic(amount) # åºéé¡ã確èª
if amount <= 0:
ic("Withdrawal amount must be positive")
return False
if self.balance >= amount:
self.balance -= amount
ic(self.balance) # æŽæ°åŸã®æ®é«ã確èª
return True
else:
ic("Insufficient funds") # æ®é«äžè¶³ã¡ãã»ãŒãžã確èª
return False
account = BankAccount(100)
account.deposit(50)
account.withdraw(30)
account.withdraw(150)
account.deposit(-10) # äžæ£ãªå€
å®è¡çµæãããåã¡ãœããã®åŒã³åºãæã®åŒæ°ããæ®é«ã®å€åããšã©ãŒãã³ããªã³ã°ã®ç¶æ³ãæã«åãããã«ããããŸãã
ic| balance: 100
ic| amount: 50
ic| self.balance: 150
ic| amount: 30
ic| self.balance: 120
ic| amount: 150
ic| "Insufficient funds"
ic| amount: -10
ic| "Deposit amount must be positive"
ã«ãŒãå ã§ã®æŽ»çš
ã«ãŒãã®åã€ãã¬ãŒã·ã§ã³ã§ã®å€ã®å€åã確èªãããå Žåã«ã `ic()` ã¯åœ¹ç«ã¡ãŸãã
from icecream import ic
my_list = ["apple", "banana", "cherry"]
my_dict = {"a": 1, "b": 2, "c": 3}
ic("Processing list:")
for i, fruit in enumerate(my_list):
ic(i, fruit)
if fruit == "banana":
ic("Found banana!")
ic("Processing dictionary:")
for key, value in my_dict.items():
processed_value = value * 10
ic(key, value, processed_value)
å®è¡çµæïŒ
ic| "Processing list:"
ic| i: 0, fruit: 'apple'
ic| i: 1, fruit: 'banana'
ic| "Found banana!"
ic| i: 2, fruit: 'cherry'
ic| "Processing dictionary:"
ic| key: 'a', value: 1, processed_value: 10
ic| key: 'b', value: 2, processed_value: 20
ic| key: 'c', value: 3, processed_value: 30
ã«ãŒãã«ãŠã³ã¿ãåèŠçŽ ã®å€ãç¹å®ã®æ¡ä»¶ã«åèŽããã¿ã€ãã³ã°ãªã©ããã©ã®ã€ãã¬ãŒã·ã§ã³ã®ãã®ãæ確ã«ããããŸãã
ããŒã¿æ§é ã®ç¢ºèª (Pandasãªã©)
Pandasã®DataFrameãªã©ãè€éãªããŒã¿æ§é ã®äžèº«ã確èªããéã«ãã`ic()` ã¯èŠããã衚瀺ããŠãããŸãã
import pandas as pd
from icecream import ic
# ãµã³ãã«ããŒã¿ãäœæ
data = {
'ID': [101, 102, 103, 104, 105],
'Product': ['Apple', 'Banana', 'Orange', 'Apple', 'Banana'],
'Price': [1.2, 0.5, 0.8, 1.3, 0.6],
'Quantity': [100, 150, 200, 120, 180]
}
df = pd.DataFrame(data)
ic(df.head(3)) # å
é 3è¡ã衚瀺
ic(df.shape) # DataFrameã®åœ¢ç¶ (è¡æ°, åæ°)
ic(df.dtypes) # ååã®ããŒã¿å
ic(df.describe()) # åºæ¬çµ±èšé
# ç¹å®ã®åŠççµæã確èª
total_revenue = (df['Price'] * df['Quantity']).sum()
ic(total_revenue)
average_price_apple = df[df['Product'] == 'Apple']['Price'].mean()
ic(average_price_apple)
å®è¡çµæïŒäžéšæç²ïŒïŒ
ic| df.head(3): ID Product Price Quantity
0 101 Apple 1.2 100
1 102 Banana 0.5 150
2 103 Orange 0.8 200
ic| df.shape: (5, 4)
ic| df.dtypes: ID int64
Product object
Price float64
Quantity int64
dtype: object
ic| df.describe(): ID Price Quantity
count 5.000000 5.000000 5.000000
mean 103.000000 0.880000 150.000000
std 1.581139 0.334664 38.078866
min 101.000000 0.500000 100.000000
25% 102.000000 0.600000 120.000000
50% 103.000000 0.800000 150.000000
75% 104.000000 1.200000 180.000000
max 105.000000 1.300000 200.000000
ic| total_revenue: 619.0
ic| average_price_apple: 1.25
DataFrameã®æŠèŠæ å ±ããããŒã¿åæéäžã®èšç®çµæãªã©ãç°¡åã«ç¢ºèªã§ããããŒã¿ãµã€ãšã³ã¹ãããžã§ã¯ãã§ã®ãããã°ã«ãéåžžã«æå¹ã§ãã
ååž°é¢æ°ã®ãããã°
ååž°é¢æ°ã®ãããã°ã¯ãåŒã³åºãã®æ·±ããåã¹ãããã§ã®å€ã®å€åãè¿œãã®ãé£ããå ŽåããããŸããã`ic()` ã䜿ãã°æ Œæ®µã«åããããããªããŸãããã£ããããæ°åãèšç®ããé¢æ°ã§è©ŠããŠã¿ãŸãããã
from icecream import ic
# ååž°åŒã³åºãã®æ·±ãã远跡ããã«ãŠã³ã¿ãŒ
recursion_depth = 0
def fibonacci(n):
global recursion_depth
prefix = " " * recursion_depth # æ·±ãã«å¿ããŠã€ã³ãã³ã
ic.configureOutput(prefix=f"{prefix}ic| ") # ã€ã³ãã³ãä»ããã¬ãã£ãã¯ã¹ãèšå®
ic(n) # ååŒã³åºãæã®nã®å€ã確èª
recursion_depth += 1
if n <= 1:
result = n
else:
result = fibonacci(n-1) + fibonacci(n-2)
recursion_depth -= 1
ic.configureOutput(prefix=f"{' ' * recursion_depth}ic| ") # æ»ããšãã«ã€ã³ãã³ããæ»ã
ic(n, result) # åã¹ãããã®nãšèšç®çµæã確èª
return result
# å®è¡åã«ãã¬ãã£ãã¯ã¹ããªã»ãã
ic.configureOutput(prefix="ic| ")
fibonacci(4)
å®è¡çµæïŒ
ic| n: 4
ic| n: 3
ic| n: 2
ic| n: 1
ic| n: 1, result: 1
ic| n: 0
ic| n: 0, result: 0
ic| n: 2, result: 1
ic| n: 1
ic| n: 1, result: 1
ic| n: 3, result: 2
ic| n: 2
ic| n: 1
ic| n: 1, result: 1
ic| n: 0
ic| n: 0, result: 0
ic| n: 2, result: 1
ic| n: 4, result: 3
ã€ã³ãã³ããšçµã¿åãããããšã§ãååž°åŒã³åºãã®éå±€æ§é ãšåã¬ãã«ã§ã®èšç®çµæãèŠèŠçã«ç解ãããããªããŸãã
ã«ã¹ã¿ãã€ãºãšé«åºŠãªäœ¿ãæ¹âïž
`icecream` ã¯ããã©ã«ãã§ãåå䟿å©ã§ãããããã«çŽ°ããæåãã«ã¹ã¿ãã€ãºããããšãå¯èœã§ãã
åºåã®æå¹å/ç¡å¹å
ãããã°ãå®äºããåŸããã¹ãŠã® `ic()` åºåãäžæçã«æ¢ãããå ŽåããããŸãããã®éã¯ãã³ãŒãã®åé ãªã©ã§ `ic.disable()` ãåŒã³åºããŸãã
from icecream import ic
# ãã¹ãŠã®ic()åºåãç¡å¹å
ic.disable()
x = 10
ic(x) # ããã¯è¡šç€ºãããªã
# ç¹å®ã®ç®æã ãæå¹åãããå Žå
ic.enable()
y = 20
ic(y) # ããã¯è¡šç€ºããã
# å床ç¡å¹å
ic.disable()
z = 30
ic(z) # ããã¯è¡šç€ºãããªã
ããã«ããã`print()` æã®ããã«äžã€äžã€ã³ã¡ã³ãã¢ãŠããããåé€ãããããæéãçããŸãããããã°ãå¿ èŠã«ãªã£ãã `ic.enable()` ã `ic.disable()` ã®è¡ãã³ã¡ã³ãã¢ãŠã/解é€ããã ãã§æžã¿ãŸããæ¬çªãªãªãŒã¹åã« `ic.disable()` ãã³ãŒãã®æåã«å ¥ããŠããã°ããããã°çšã® `ic()` åŒã³åºããåé€ãå¿ããŠãåºåãããå¿é ããããŸããã
åºåèšå®ã®ã«ã¹ã¿ãã€ãº: `ic.configureOutput()`
`ic.configureOutput()` ã¡ãœããã䜿ããšãåºåã®èŠãç®ãæåããã现ããå¶åŸ¡ã§ããŸããäž»èŠãªèšå®é ç®ãèŠãŠãããŸãããã
ãã©ã¡ãŒã¿å | 説æ | ããã©ã«ãå€ | äŸ |
---|---|---|---|
prefix | åºåã®å é ã«ã€ãæååãé¢æ°ãæå®ããŸããé¢æ°ãæå®ãããšãåŒã³åºãããšã«è©äŸ¡ãããŸããã¿ã€ã ã¹ã¿ã³ããã«ã¹ã¿ã ã¡ãã»ãŒãžã®æ¿å ¥ã«äŸ¿å©ã§ãã | 'ic| ' | ic.configureOutput(prefix='DEBUG: ') import time; ic.configureOutput(prefix=lambda: f'{time.strftime("%H:%M:%S")} >> ') |
outputFunction | åºåã«äœ¿çšããé¢æ°ãæå®ããŸããæšæºãšã©ãŒåºå (stderr) ãæšæºåºå (stdout)ããã®ã³ã°ã©ã€ãã©ãªã®é¢æ°ããã¡ã€ã«æžã蟌ã¿çšã®é¢æ°ãªã©ã«å€æŽã§ããŸãã | å éšé¢æ° (ããã©ã«ãã§stderrã«åºå) | import sys; ic.configureOutput(outputFunction=lambda s: sys.stdout.write(s + '\\n')) import logging; logging.basicConfig(level=logging.DEBUG); ic.configureOutput(outputFunction=logging.debug) log_file = open('debug.log', 'a'); ic.configureOutput(outputFunction=lambda s: log_file.write(s + '\\n')) |
argToStringFunction | åŒæ°ãæååã«å€æããé¢æ°ãæå®ããŸããæšæºã® `repr()` ã䜿ã£ãããç¹å®ã®ã«ã¹ã¿ã ã¯ã©ã¹ã®ãªããžã§ã¯ããåããããã圢åŒã§è¡šç€ºãããããå Žåã«äŸ¿å©ã§ãã | `icecream` å éšã®æŽåœ¢é¢æ° | ic.configureOutput(argToStringFunction=repr)
|
includeContext | True ã«ãããšãåŒæ°ããã® `ic()` åŒã³åºãã§ããã¡ã€ã«åãè¡çªå·ã芪é¢æ°åãªã©ã®ã³ã³ããã¹ãæ
å ±ãåžžæ衚瀺ããŸããã©ã® `ic()` ãåŒã°ããã远跡ãããããªããŸãã | False | ic.configureOutput(includeContext=True) |
contextAbsPath | includeContext=True ã®ãšãããã¡ã€ã«ãã¹ã絶察ãã¹ã§è¡šç€ºãããã©ãããæå®ããŸããTrue ã«ãããšãVSCodeãªã©ã®ãšãã£ã¿ã§ãã¹ãã¯ãªãã¯ããŠè©²åœç®æã«çŽæ¥ãžã£ã³ãã§ããããããããã°å¹çãåäžããŸãã | False | ic.configureOutput(includeContext=True, contextAbsPath=True) |
ãããã®èšå®ãçµã¿åãããããšã§ããããžã§ã¯ãã®èŠä»¶ãå人ã®å¥œã¿ã«åããããããã°åºåç°å¢ãæ§ç¯ã§ããŸãã
äŸïŒãã¬ãã£ãã¯ã¹ã«ã¿ã€ã ã¹ã¿ã³ããã€ãããã°ãã¡ã€ã«ã«åºåããã³ã³ããã¹ãæ å ±ïŒçµ¶å¯Ÿãã¹ïŒã衚瀺ããèšå®
import time
import os
from icecream import ic
# ãã°ãã¡ã€ã«ã®èšå®
log_filename = 'app_debug.log'
log_file = open(log_filename, 'a', encoding='utf-8')
def get_timestamp_prefix():
return f'{time.strftime("%Y-%m-%d %H:%M:%S")} | '
# icecreamã®èšå®
ic.configureOutput(
prefix=get_timestamp_prefix,
outputFunction=lambda s: log_file.write(s + '\\n'),
includeContext=True,
contextAbsPath=True
)
# --- ãã以éã® ic() åŒã³åºãã¯äžèšèšå®ã§ãã°ãã¡ã€ã«ã«èšé²ããã ---
def calculate_something(x, y):
ic("Starting calculation...")
result = (x * x) + (y * y)
ic(x, y, result)
ic("Calculation finished.")
return result
if __name__ == "__main__":
ic("Script started.")
res1 = calculate_something(5, 3)
res2 = calculate_something(10, -2)
ic("Script finished.", res1, res2)
log_file.close() # ã¹ã¯ãªããçµäºæã«ãã¡ã€ã«ãéãã
ãã®ã¹ã¯ãªãããå®è¡ãããšã`app_debug.log` ãã¡ã€ã«ã«ä»¥äžã®ãããªåœ¢åŒã§ãã°ãè¿œèšãããŸãïŒãã¹ã¯ç°å¢ã«ããç°ãªããŸãïŒã
2025-04-05 07:56:00 | /path/to/your/script.py:30 in <module> - "Script started."
2025-04-05 07:56:00 | /path/to/your/script.py:23 in calculate_something() - "Starting calculation..."
2025-04-05 07:56:00 | /path/to/your/script.py:25 in calculate_something() - x: 5, y: 3, result: 34
2025-04-05 07:56:00 | /path/to/your/script.py:26 in calculate_something() - "Calculation finished."
2025-04-05 07:56:00 | /path/to/your/script.py:23 in calculate_something() - "Starting calculation..."
2025-04-05 07:56:00 | /path/to/your/script.py:25 in calculate_something() - x: 10, y: -2, result: 104
2025-04-05 07:56:00 | /path/to/your/script.py:26 in calculate_something() - "Calculation finished."
2025-04-05 07:56:00 | /path/to/your/script.py:32 in <module> - "Script finished.", res1: 34, res2: 104
ãã€ãã©ãã§ãã©ã®å€ãåºåãããããéåžžã«æ確ã«ãªãããã¡ã€ã«ãžã®ãã®ã³ã°ãç°¡åã«è¡ããŸããïŒ
IceCream vs ä»ã®ãããã°ææ³ ð
`icecream` ã¯éåžžã«äŸ¿å©ã§ãããPythonã«ã¯ä»ã«ããããã°ããŒã«ãååšããŸããããããã®ç¹åŸŽãç解ããç¶æ³ã«å¿ããŠæé©ãªããŒã«ãéžæããããšãéèŠã§ãã
ææ³ | ã¡ãªãã | ãã¡ãªãã | é©ããå Žé¢ |
---|---|---|---|
`print()` | ã»æšæºæ©èœã§æ軜 ã»çŽæçã§åŠç¿ã³ã¹ããäœã | ã»å€æ°åãæåã§å
¥ããå¿
èŠãã ã»åºåãå€ããªããšèŠã«ãã ã»åé€/ã³ã¡ã³ãã¢ãŠããæé ã»ããŒã¿æ§é ãèŠã«ãã | ã»éåžžã«åçŽãªã¹ã¯ãªãã ã»äœ¿ãæšãŠã®ç°¡åãªå€ç¢ºèª |
`icecream` (`ic()`) | ã»å€æ°åãšå€ãèªå衚瀺⚠ã»ã³ã³ããã¹ãæ å ±è¡šç€º (ãã¡ã€ã«å/è¡/é¢æ°)ð ã»èŠãããæŽåœ¢åºå (Pretty-print)ð ã»äžæ¬æå¹/ç¡å¹åã容æ (disable/enable)â ã»åºåã«ã¹ã¿ãã€ãºå¯èœ (prefix/output/context)ð§ ã»ã¿ã€ãéãå°ãªãâšïž | ã»ã©ã€ãã©ãªã®ã€ã³ã¹ããŒã«ãå¿ èŠ (`pip install icecream`) | ã»æ¥åžžçãªéçºã»ãããã°å
šè¬ ã»è€éãªããŒã¿æ§é ãåŠçãããŒã®è¿œè·¡ ã»printãããã°ã®å¹çåã»ä»£æ¿ ã»ããŒã¿ãµã€ãšã³ã¹ã®å®éšã»åæéçš |
`logging` ã¢ãžã¥ãŒã« | ã»æšæºã©ã€ãã©ãª ã»ãã°ã¬ãã«èšå®å¯èœ (DEBUG, INFO, WARNING, ERROR, CRITICAL) ã»ãã¡ã€ã«åºåãããŒããŒã·ã§ã³ãªã©æè»ãªãã³ãã© ã»ãã©ãŒãããæå®å¯èœ ã»æ¬çªç°å¢ã§ã®ãã°èšé²ã«ãäœ¿çš | ã»èšå®ã `print` ã `ic` ããããè€é ã»äžæçãªãããã°ã«ã¯åé·ãªå Žåã ã»å€æ°åãšå€ãèªåã§ã¯è¡šç€ºããªã | ã»æ°žç¶çãªãã°èšé²ãå¿
èŠãªå Žå ã»ãã°ã¬ãã«ã«å¿ããåºåå¶åŸ¡ ã»æ¬çªç°å¢ã§ã®éçšãã°ããšã©ãŒè¿œè·¡ ã»ã©ã€ãã©ãªéçºæã®ãã°æäŸ |
`pdb` (Python Debugger) | ã»æšæºã©ã€ãã©ãª ã»ã¹ãããå®è¡ (next, step, continue)ð¶ ã»ãã¬ãŒã¯ãã€ã³ãèšå® ã»å®è¡äžã®å€æ°æ€æ»ã»å€æŽ ã»ã³ãŒã«ã¹ã¿ãã¯ã®ç¢ºèª ã»å¯Ÿè©±çãªãããã° | ã»CUIæäœã«æ
£ããå¿
èŠ ã»ããã°ã©ã ã®å®è¡ãäžæåæ¢ããå¿ èŠããã ã»`ic` ã®ãããªãæµãèŠãçãªãããã°ã«ã¯äžåã | ã»è€éãªãã°ã®åå ç¹å® ã»ããã°ã©ã ã®å®è¡ãã¹ãããããšã«è¿œãããå Žå ã»å®è¡æã®ç¶æ ã詳现ã«èª¿æ»ãããå Žå ã»ç¹å®ã®æ¡ä»¶äžã§ã®ã¿çºçããåé¡ã®è§£æ |
IDEçµ±åããã㬠(VSCode, PyCharmãªã©) | ã»`pdb` ã®æ©èœãGUIã§æäŸ ã»èŠèŠçãªãã¬ãŒã¯ãã€ã³ãèšå®ð±ïž ã»å€æ°ãŠã©ãããåŒã®è©äŸ¡ ã»ã³ãŒã«ã¹ã¿ãã¯ã®èŠèŠç衚瀺 ã»æ¡ä»¶ä»ããã¬ãŒã¯ãã€ã³ã ã»é«æ©èœã§äœ¿ãããã | ã»IDEã®ã»ããã¢ãããå¿
èŠ ã»ãªã¢ãŒããããã°ãªã©ã¯èšå®ãè€éãªå Žåã ã»ãšãã£ã¿ã«ãã£ãŠã¯å©çšäžå¯ | ã»`pdb` ãšåæ§ã®å Žé¢ ã»GUIã§ã®ãããã°ã奜ãå Žå ã»å€§èŠæš¡ãããžã§ã¯ã ã»è€éãªã¢ããªã±ãŒã·ã§ã³ã®ãããã° |
çµè«ãšããŠã`icecream` 㯠`print()` ãããã°ã®æ軜ããå€§å¹ ã«åäžããã åªããéžæè¢ã§ããæ¥åžžçãªéçºããã³ãŒãã®æåãçŽ æ©ã確èªãããå Žåã«ã`print()` ãã `ic()` ã«çœ®ãæããã ãã§ãéçºå¹çã倧ããåäžããå¯èœæ§ããããŸããç¹ã«ãå€æ°åãã³ã³ããã¹ããèªåã§è¡šç€ºãããç¹ã¯ã`print()` ã«ã¯ãªã倧ããªå©ç¹ã§ãã
äžæ¹ã§ãããã°ã©ã ã®å®è¡ãæ¢ããŠã¹ãããããšã«è¿œè·¡ãããããããã¯å®è¡äžã®å€æ°ãã€ã³ã¿ã©ã¯ãã£ãã«å€æŽããããšãã£ãããã詳现ãªãããã°ãå¿ èŠãªå Žå㯠`pdb` ãIDEãããã¬ã匷åãªããŒã«ãšãªããŸãããŸããã¢ããªã±ãŒã·ã§ã³ã®éçšãèŠæ®ããæ°žç¶çãªãã°èšé²ãããã°ã¬ãã«ã«ãã管çãå¿ èŠãªå Žå㯠`logging` ã¢ãžã¥ãŒã«ãé©ããŠããŸãã
ããããã®ããŒã«ã®åŸæãªé åãç解ããç®çã«å¿ããŠãããã䜿ãåããããããã¯çµã¿åãããããšããå¹ççãªPythonéçºã®éµãšãªããŸãã `icecream` ã¯ããã®äžã§ãç¹ã«ãprintãããã°ãã®é åã匷åã«ãµããŒãããŠããããé Œããã仲éãšèšããã§ãããã
ãŸãšã ð
`icecream` ã©ã€ãã©ãªã¯ãPythonã«ããããããã°äœæ¥ãåçã«æ¹åããå¯èœæ§ãç§ãããã·ã³ãã«ãã€åŒ·åãªããŒã«ã§ããéå±ã§æéã®ããã `print()` ãããã°ããããªãã解æŸããããå¿«é©ã§å¹ççãªéçºäœéšããããããŸãã
ãã®èšäºã§è§£èª¬ãã `icecream` ã®äž»ãªã¡ãªãããå確èªããŸãããïŒ
- â å€æ°åãšå€ã®èªå衚瀺ã§ãé¢å㪠`f-string` ã `%` ãã©ãŒããããã解æŸïŒ
- ð ã³ã³ããã¹ãæ å ±ïŒãã¡ã€ã«åãè¡çªå·ãé¢æ°åïŒã§ãåºåãã©ãããæ¥ããäžç®çç¶ïŒ
- ð ããŒã¿æ§é ã®èŠãããæŽåœ¢åºåã§ããªã¹ããèŸæžã楜ã ãã§ãã¯ïŒ
- â `ic.disable()` / `ic.enable()` ã§ããããã°åºåã®ãªã³/ãªããè¶ ç°¡åïŒ
- ð§ `ic.configureOutput()` ã«ããæè»ãªã«ã¹ã¿ãã€ãºã§ãåºå圢åŒãåºåå ãèªç±èªåšïŒ
ã€ã³ã¹ããŒã«ã `pip install icecream` ã ãã§ç°¡åã«è¡ãã`from icecream import ic` ãšã€ã³ããŒãããã°ããã«äœ¿ãå§ããããŸããåŠç¿ã³ã¹ããéåžžã«äœãã`print()` ã䜿ãæèŠã§ `ic()` ã䜿ãã ãã§ãã
ããããªãããŸã `print()` é¢æ°ããããã°ã®äž»æŠåãšããŠãããªãããã²äžåºŠ `icecream` ãè©ŠããŠã¿ãŠãã ããããã£ãšãã®æ軜ããšæ å ±éã®å€ãã«é©ããããã print() ãããã°ã«ã¯æ»ããªãïŒããšæããã¯ãã§ããðŠð
æ¥ã ã®ã³ãŒãã£ã³ã°ãå°ãã§ãå¿«é©ã§ãçç£çã«ãªãããšãé¡ã£ãŠããŸããHappy Debugging! ð
ã³ã¡ã³ã