Pythonデフォルト引数完全ガイド【初心者向け解説と注意点】
Pythonプログラミングにおいて、デフォルト引数は関数を柔軟に設计する重要な機能です。この記事では、デフォルト引数の基本から応用、よく発生する問題とその対策まで分かりやすく解説します。
デフォルト引数とは?
デフォルト引数とは、関数定義時に引数に初期値を設定する機能です。関数呼び出し時にその引数を省略した場合、デフォルト値が自動的に使用されます。
基本的なデフォルト引数の使い方
最もシンプルな例
def greet(name="世界"):
print(f"こんにちは、{name}!")
greet() # デフォルト値を使用
greet("田中") # 引数を指定
出力:
こんにちは、世界!
こんにちは、田中!
複数のデフォルト引数
def create_profile(name, age=20, city="東京"):
print(f"名前: {name}, 年齢: {age}, 出身: {city}")
create_profile("佐藤")
create_profile("山田", 25)
create_profile("田中", 30, "大阪")
出力:
名前: 佐藤, 年齢: 20, 出身: 東京
名前: 山田, 年齢: 25, 出身: 東京
名前: 田中, 年齢: 30, 出身: 大阪
デフォルト引数の配置ルール
正しい配置
# 正しい:必須引数 → デフォルト引数
def correct_order(name, age=25, city="東京"):
return f"{name}({age}歳) - {city}"
print(correct_order("鈴木"))
出力:
鈴木(25歳) - 東京
間違った配置
# 間違い:デフォルト引数の後に必須引数は配置できない
# def wrong_order(name="匿名", age): # SyntaxError
# pass
キーワード引数との組み合わせ
特定の引数のみ指定
def book_info(title, author="不明", year=2024, pages=100):
print(f"『{title}』 著者: {author} ({year}年, {pages}ページ)")
book_info("Python入門")
book_info("データ分析", year=2023)
book_info("機械学習", author="AI太郎", pages=250)
出力:
『Python入門』 著者: 不明 (2024年, 100ページ)
『データ分析』 著者: 不明 (2023年, 100ページ)
『機械学習』 著者: AI太郎 (2024年, 250ページ)
実践的なデフォルト引数の活用例
データベース接続関数
def connect_db(host="localhost", port=5432, database="mydb", timeout=30):
print(f"接続中: {host}:{port}/{database} (タイムアウト: {timeout}秒)")
return {"status": "connected", "host": host, "port": port}
# 様々な呼び出し方法
connect_db()
connect_db("remote-server")
connect_db(port=3306, database="testdb")
出力:
接続中: localhost:5432/mydb (タイムアウト: 30秒)
接続中: remote-server:5432/mydb (タイムアウト: 30秒)
接続中: localhost:3306/testdb (タイムアウト: 30秒)
ファイル読み込み関数
def read_file(filename, encoding="utf-8", mode="r"):
print(f"ファイル '{filename}' を {encoding} エンコーディングで開いています")
# 実際の実装では以下のようになります
# with open(filename, mode, encoding=encoding) as f:
# return f.read()
return f"'{filename}' の内容"
content1 = read_file("data.txt")
content2 = read_file("shift_jis.txt", encoding="shift_jis")
出力:
ファイル 'data.txt' を utf-8 エンコーディングで開いています
ファイル 'shift_jis.txt' を shift_jis エンコーディングで開いています
HTTP リクエスト関数
def make_request(url, method="GET", timeout=10, headers=None):
if headers is None:
headers = {"User-Agent": "Python-Client"}
print(f"{method} {url} (タイムアウト: {timeout}秒)")
print(f"ヘッダー: {headers}")
return {"status": 200, "data": "response"}
make_request("https://api.example.com")
make_request("https://api.example.com/users", method="POST", timeout=20)
出力:
GET https://api.example.com (タイムアウト: 10秒)
ヘッダー: {'User-Agent': 'Python-Client'}
POST https://api.example.com/users (タイムアウト: 20秒)
ヘッダー: {'User-Agent': 'Python-Client'}
デフォルト引数の落とし穴と対策
【危険】ミュータブルオブジェクトをデフォルト値に使用
# 間違った例:リストをデフォルト値に使用
def add_item_wrong(item, items=[]):
items.append(item)
return items
# 問題のある動作
list1 = add_item_wrong("りんご")
list2 = add_item_wrong("バナナ")
print("list1:", list1)
print("list2:", list2)
出力:
list1: ['りんご']
list2: ['りんご', 'バナナ'] # 期待しない結果!
【正解】Noneを使用した安全な実装
def add_item_correct(item, items=None):
if items is None:
items = []
items.append(item)
return items
# 正しい動作
list1 = add_item_correct("りんご")
list2 = add_item_correct("バナナ")
print("list1:", list1)
print("list2:", list2)
出力:
list1: ['りんご']
list2: ['バナナ'] # 期待通りの結果
辞書の場合も同様
def create_config(name, options=None):
if options is None:
options = {}
options["name"] = name
options["created"] = True
return options
config1 = create_config("設定1")
config2 = create_config("設定2", {"debug": True})
print("config1:", config1)
print("config2:", config2)
出力:
config1: {'name': '設定1', 'created': True}
config2: {'debug': True, 'name': '設定2', 'created': True}
関数オブジェクトをデフォルト値に使用
from datetime import datetime
def log_message(message, timestamp=None):
if timestamp is None:
timestamp = datetime.now()
print(f"[{timestamp}] {message}")
# 呼び出すたびに現在時刻が使用される
log_message("開始")
log_message("終了")
クラスメソッドでのデフォルト引数
class Calculator:
def __init__(self, precision=2):
self.precision = precision
def divide(self, a, b, round_result=True):
result = a / b
if round_result:
return round(result, self.precision)
return result
calc = Calculator()
print(calc.divide(10, 3))
print(calc.divide(10, 3, round_result=False))
出力:
3.33
3.3333333333333335
デフォルト引数と*args、**kwargsの組み合わせ
def flexible_function(required_arg, default_arg="デフォルト", *args, **kwargs):
print(f"必須引数: {required_arg}")
print(f"デフォルト引数: {default_arg}")
print(f"可変位置引数: {args}")
print(f"可変キーワード引数: {kwargs}")
flexible_function("必須", "カスタム", 1, 2, 3, key1="値1", key2="値2")
出力:
必須引数: 必須
デフォルト引数: カスタム
可変位置引数: (1, 2, 3)
可変キーワード引数: {'key1': '値1', 'key2': '値2'}
デフォルト引数の確認方法
def sample_function(a, b=10, c="hello"):
return a + b
# 関数のデフォルト値を確認
print("デフォルト値:", sample_function.__defaults__)
print("引数名:", sample_function.__code__.co_varnames)
出力:
デフォルト値: (10, 'hello')
引数名: ('a', 'b', 'c')
エラーハンドリングとデフォルト引数
def safe_divide(a, b=1, handle_error=True):
try:
return a / b
except ZeroDivisionError:
if handle_error:
print("ゼロ除算エラーが発生しました")
return None
else:
raise
print(safe_divide(10)) # b=1がデフォルト
print(safe_divide(10, 0)) # エラーハンドリング有効
print(safe_divide(10, 2, False)) # エラーハンドリング無効
出力:
10.0
ゼロ除算エラーが発生しました
None
5.0
デフォルト引数のベストプラクティス
1. イミュータブルな値のみ使用
# 良い例
def good_defaults(name, count=0, active=True, message=""):
return {"name": name, "count": count, "active": active, "message": message}
# 避けるべき例
# def bad_defaults(name, items=[], metadata={}): # リストや辞書は避ける
# pass
2. 意味のあるデフォルト値の設定
def send_email(to, subject="お知らせ", from_email="noreply@example.com", priority="normal"):
print(f"送信: {to} / 件名: {subject} / 送信者: {from_email} / 優先度: {priority}")
send_email("user@example.com")
send_email("admin@example.com", "重要なお知らせ", priority="high")
出力:
送信: user@example.com / 件名: お知らせ / 送信者: noreply@example.com / 優先度: normal
送信: admin@example.com / 件名: 重要なお知らせ / 送信者: noreply@example.com / 優先度: high
3. ドキュメントでデフォルト値を明記
def process_data(data, format_type="json", validate=True, timeout=30):
"""
データを処理する関数
Args:
data: 処理対象のデータ
format_type (str): データ形式 (デフォルト: "json")
validate (bool): バリデーション実行フラグ (デフォルト: True)
timeout (int): タイムアウト秒数 (デフォルト: 30)
"""
print(f"データ処理: 形式={format_type}, 検証={validate}, タイムアウト={timeout}秒")
process_data("sample_data")
出力:
データ処理: 形式=json, 検証=True, タイムアウト=30秒
よくあるエラーと対処法
1. デフォルト引数の順序エラー
# エラー例
# def wrong_function(name="匿名", age): # SyntaxError
# pass
# 正しい実装
def correct_function(age, name="匿名"):
return f"{name}({age}歳)"
print(correct_function(25))
出力:
匿名(25歳)
2. ミュータブルデフォルト引数の問題
# 問題のあるコード
def append_to_list(item, target_list=[]):
target_list.append(item)
return target_list
# 解決方法
def append_to_list_safe(item, target_list=None):
if target_list is None:
target_list = []
target_list.append(item)
return target_list
まとめ
Pythonデフォルト引数は以下の点で非常に有用です:
- 関数の使いやすさ向上: よく使用される値をデフォルト値に設定
- 後方互換性の維持: 既存コードを壊さずに新しい引数を追加
- コードの簡潔性: 頻繁に同じ値を渡す必要がない
重要な注意点:
- ミュータブルオブジェクト(リスト、辞書など)をデフォルト値に使用しない
- デフォルト引数は必須引数の後に配置する
- 意味のあるデフォルト値を設定する
デフォルト引数を適切に使用することで、より柔軟で使いやすい関数を作成できます。実際にコードを書いて、この便利な機能をマスターしましょう。
関連キーワード
- Python デフォルト引数
- Python 関数 引数
- デフォルト値 Python
- キーワード引数 Python
- 関数 パラメータ
- Python 初心者
- ミュータブル デフォルト引数
- Python 関数設計
- 引数 省略
■プロンプトだけでオリジナルアプリを開発・公開してみた!!
■AI時代の第一歩!「AI駆動開発コース」はじめました!
テックジム東京本校で先行開始。
■テックジム東京本校
「武田塾」のプログラミング版といえば「テックジム」。
講義動画なし、教科書なし。「進捗管理とコーチング」で効率学習。
より早く、より安く、しかも対面型のプログラミングスクールです。
<短期講習>5日で5万円の「Pythonミニキャンプ」開催中。
<月1開催>放送作家による映像ディレクター養成講座
<オンライン無料>ゼロから始めるPython爆速講座

