*args は複数の名前のないパラメータ (タプル) を表し、**kwargs はキーワード パラメータ (辞書) を表します。
def fun(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '###' if __name__ == '__main__': foo(1,2,3,4) foo(a=1,b=2,c=3) foo(1,2,3,4, a=1,b=2,c=3) foo('a', 1, None, a=1, b='2', c=3)
出力結果は以下の通りです:
args = (1, 2, 3, 4) kwargs = {} ### args = () kwargs = {'a': 1, 'c': 3, 'b': 2} ### args = (1, 2, 3, 4) kwargs = {'a': 1, 'c': 3, 'b': 2} ### args = ('a', 1, None) kwargs = {'a': 1, 'c': 3, 'b': '2'} ###
ご覧のとおり、これら 2 つは Python の可変パラメータです。
注: *args と **kwargs を同時に使用する場合は、foo(a=1, b='2', c=3, a', のように *args パラメーターを **kwargs の前にリストする必要があります。 1、None 、 )、この方法で呼び出すと、構文エラー「SyntaxError: non-keyword arg after keyword arg」が表示されます。
def fun2(param1, *args, **kwargs): print 'param1 = ', param1 print 'args = ', args print 'kwargs = ', kwargs print '###' fun2(1, 2, 3, 4, a=1,b=2,c=3)
出力結果:
param1 = 1 args = (2,3,4) kwargs = {'a': 1, 'c': 3, 'b': 2} ###
1はparam1に割り当てられ、残りの2、3、4は*argsに割り当てられ、その他は**kwargsに割り当てられます
非常に美しい使い方もあります。辞書を作成することです:
def kw_dict(**kwargs): return kwargs print kw_dict(a=1,b=2,c=3)
結果:
{'a':1, 'b':2, 'c':3}
実際、Pythonにはdictクラスがあり、dict(a=1,b=2,c=3)を使用して辞書を作成できます。
以上がPythonにおける*argsと**kwargsの使い方の比較説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

Dreamweaver Mac版
ビジュアル Web 開発ツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

WebStorm Mac版
便利なJavaScript開発ツール
