Python でオプションのパラメーターを関数に渡すにはどうすればよいですか?

Python でオプションのパラメーターを関数に渡すにはどうすればよいですか?

Python では、特定のパラメーターのデフォルト値を使用して関数を定義すると、その引数はユーザーのオプションとして設定されると言われます。ユーザーは自分の値を渡すことも、関数が指定されたデフォルト値を使用するように振る舞うこともできます。

このように、ユーザーはオプションのパラメーターを渡すか、必須パラメーターのみを渡すことによって関数を呼び出すことができます。

Pythonでオプションのパラメータを渡すには主に2つの方法があります

  • キーワード引数を使用しない場合。
  • キーワード引数を使用する。

キーワード引数を使用せずに渡す

キーワード引数を使用せずに渡すときに注意すべき主な点は次のとおりです。

  • パラメータの順序は維持される必要があります。つまり、関数内でパラメータが定義されている順序は、関数の呼び出し中に維持される必要があります。
  • オプションではないパラメータの値を渡す必要があります。それ以外の場合は、エラーがスローされます。
  • デフォルトの引数の値は、渡すことも無視することもできます。

以下に、この概念を説明するコードをいくつか示します。

例 1:

Python3




# Here b is predefined and hence is optional.> def> func(a, b> => 1098> ):> > return> a> +> b> print> (func(> 2> ,> 2> ))> # this 1 is represented as 'a' in the function and> # function uses the default value of b> print> (func(> 1> ))>

出力:

4 1099 

例 2: 文字列を渡すこともできます。

Python3




# Here string2 is the default string used> def> fun2(string1, string2> => 'Geeks'> ):> > print> (string1> +> string2)> # calling the function using default value> fun2(> 'GeeksFor'> )> # calling without default value.> fun2(> 'GeeksFor'> ,> 'Geeks'> )>

出力:

GeeksForGeeks GeeksForGeeks 

キーワード引数を指定して渡す

関数を定義する場合、パラメータはデータ型キーワード名の形式で記述されます。したがって、Python は、値を渡すためのキーワード名を使用して関数を呼び出すメカニズムを提供します。これにより、プログラマは、パラメータが渡されるシーケンスや順序を学習する必要がなくなります。

覚えておく必要がある重要な点は次のとおりです。

  • この場合、値を渡す順序を維持する必要はありません。
  • 渡されたキーワード名と宣言されたキーワード名に違いがあってはなりません。

以下はその実装のコードです。

Python3




# Here string2 is the default string used> def> fun2(string1, string2> => 'Geeks'> ):> > print> (string1> +> string2)> # Thiscan be a way where no order is needed.> fun2(string2> => 'GeeksFor'> , string1> => 'Geeks'> )> # since we are not mentioning the non-default argument> # so it will give error.> fun2(string2> => 'GeeksFor'> )>

出力:

上の例では、順序を維持する必要がないことがわかります。また、オプションのパラメーターのみを渡そうとすると、エラーが発生することがわかります。これは、オプションのパラメータにはデフォルト値があるため省略できますが、必須パラメータ (上記の場合は string1) を省略できないために発生します。そのため、フラグ付きのエラーが表示されます: missing 1 required argument.

この例では、上記のトピックについてさらに洞察を得ることができます。

Python3




def> func(a, b, c> => 'geeks'> ):> > print> (a,> 'type is'> ,> type> (a))> > print> (b,> 'type is'> ,> type> (b))> > print> (c,> 'type is'> ,> type> (c))> # The optional parameters will not decide> # the type of parameter passed.> # also the order is maintained> print> (> 'first call'> )> func(> 2> ,> 'z'> ,> 2.0> )> # below call uses the default> # mentioned value of c> print> (> 'second call'> )> func(> 2> ,> 1> )> # The below call (in comments) will give an error> # since other required parameter is not passed.> # func('a')> print> (> 'third call'> )> func(c> => 2> , b> => 3> , a> => 'geeks'> )>

出力:

first call 2 type is z type is 2.0 type is second call 2 type is 1 type is geeks type is third call geeks type is 3 type is 2 type is 

したがって、基本的に Python 関数呼び出しは、必要な数の関数パラメーターが渡されたかどうかのみをチェックします。

以下は、ユーザーが上記の両方の方法で引数を渡そうとする場合と、与えられた予防策を示しています。

Python3




def> comp(a, b> => 2> ):> > if> (a print('first parameter is smaller') if(a>b): print('2 番目のパラメータの方が小さい') if(a == b): print('両方の値が等しい。') print('最初の呼び出し') comp(1) print( '2 回目の呼び出し') comp(2, 1) print('3 回目の呼び出し') comp(b=1, a=-1) print('4 回目の呼び出し') comp(-1, b=0) )>>

>   

HTML