More Control Flow Tools,Python Tutorialノートを読む(1)


参考資料:
Python公式サイトTutorial
注:自分のPythonがまだ学んでいないような気がするので、プロジェクトのコードを見るのは難しいです.だからPython公式サイトのTutorialを見て独学で勉強したいです.私も読んでいる間に自分がすでにできるところを省略したので、私が書いたものはすべて自分で学んだ新しいものです.
規範:黒体x)は自分が学んだことを表すモジュールで、大まかな区別です.4.1,4.2は、Tutorialにおける位置を示す.
1)関数のパラメータでは,キーワードパラメータ(keyword arguments)は必ず位置パラメータ(positional arguments)の後ろにある.
2)4.7.2関数の定義では、*argumentsはメタグループの解析を表し、*argumentsは辞書の解析を表す.注意、メタグループは位置パラメータのようで、辞書はキーワードパラメータのようです.例は次のとおりです.def cheeseshop(kind, *arguments, **keywords): print("-- Do you have any", kind, "?") print("-- I'm sorry, we're all out of", kind) for arg in arguments: print(arg) print("-" * 40) for kw in keywords: print(kw, ":", keywords[kw]) このように呼び出すことができます.cheeseshop("Limburger", "It's very runny, sir.", "It's really very, VERY runny, sir.", shopkeeper="Michael Palin", client="John Cleese", sketch="Cheese Shop Sketch") 出力は次のとおりです.-- Do you have any Limburger ? -- I'm sorry, we're all out of Limburger It's very runny, sir. It's really very, VERY runny, sir. ---------------------------------------- shopkeeper : Michael Palin client : John Cleese sketch : Cheese Shop Sketch 3)4.7.3関数定義における特殊なパラメータdef f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2): ----------- ---------- ---------- | | | | Positional or keyword | | - Keyword only -- Positional only この方式は、明示的に関数定義時にパラメータ伝達の方法を規定していることに等しい.例えばdef kwd_と定義されている場合only_arg(*,arg):では、すべてのパラメータをキーワード形式で伝えるべきです!
4)4.7.7関数の文書仕様>>> def my_function(): ... """Do nothing, but document it. ... ... No, really, it doesn't do anything. ... """ ... pass ... >>> print(my_function.__doc__) Do nothing, but document it. No, really, it doesn't do anything. 4.7.8関数の注釈仕様:>>> def f(ham: str, eggs: str = 'eggs') -> str: ... print("Annotations:", f.__annotations__) ... print("Arguments:", ham, eggs) ... return ham + ' and ' + eggs ... >>> f('spam') Annotations: {'ham': , 'return': , 'eggs': } Arguments: spam eggs 'spam and eggs' コメントを使用する場合は、データ型をコロンで識別し、矢印で戻り値を識別できることがわかります.