Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
複数の関数引数
すべてのPythonの関数は、通常の宣言であれば、あらかじめ定義された数の引数を受け取ります。このように:
python
def myfunction(first, second, third):
# 3つの変数で何かを行う
...
可変数の引数を受け取る関数を宣言することも可能です。以下の構文を使用します。
python
def foo(first, second, third, *therest):
print("First: %s" % first)
print("Second: %s" % second)
print("Third: %s" % third)
print("And all the rest... %s" % list(therest))
「therest」変数は変数のリストで、「foo」関数に最初の3つの引数の後に与えられたすべての引数を受け取ります。したがって、foo(1, 2, 3, 4, 5)
と呼び出すと、次のように出力されます。
```python def foo(first, second, third, *therest): print("First: %s" %(first)) print("Second: %s" %(second)) print("Third: %s" %(third)) print("And all the rest... %s" %(list(therest)))
foo(1, 2, 3, 4, 5) ```
キーワードを使用して関数の引数を送信することも可能なので、引数の順序は重要ではなくなります。次の構文を使用します。以下のコードは次の出力を生成します:
The sum is: 6
Result: 1
```python def bar(first, second, third, **options): if options.get("action") == "sum": print("The sum is: %d" %(first + second + third))
if options.get("number") == "first":
return first
result = bar(1, 2, 3, action = "sum", number = "first") print("Result: %d" %(result)) ```
「bar」関数は3つの引数を受け取ります。追加の「action」引数が受け取られ、それが数を合計するように指示すると、合計が表示されます。あるいは、「number」パラメーターの値が「first」と等しい場合に、関数は最初の引数を返す必要があることも知っています。
エクササイズ
foo
とbar
関数を完成させて、可変数の引数(3つ以上)を受け取れるようにしましょう。 foo
関数は、受け取った追加の引数の数を返さなければなりません。 bar
は、キーワードmagicnumber
が7に相当する場合はTrue
を返し、そうでない場合はFalse
を返す必要があります。
# edit the functions prototype and implementation
def foo(a, b, c):
pass
def bar(a, b, c):
pass
# test code
if foo(1, 2, 3, 4) == 1:
print("Good.")
if foo(1, 2, 3, 4, 5) == 2:
print("Better.")
if bar(1, 2, 3, magicnumber=6) == False:
print("Great.")
if bar(1, 2, 3, magicnumber=7) == True:
print("Awesome!")
# edit the functions prototype and implementation
def foo(a, b, c, *args):
return len(args)
def bar(a, b, c, **kwargs):
return kwargs["magicnumber"] == 7
# test code
if foo(1, 2, 3, 4) == 1:
print("Good.")
if foo(1, 2, 3, 4, 5) == 2:
print("Better.")
if bar(1, 2, 3, magicnumber=6) == False:
print("Great.")
if bar(1, 2, 3, magicnumber=7) == True:
print("Awesome!")
test_output_contains("Good.")
test_output_contains("Better.")
test_output_contains("Great.")
test_output_contains("Awesome!")
success_msg("Great work!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!