Python チュートリアル - Pass ステートメント

このセクションでは、Python の pass
ステートメントを学びます。
Python の pass
ステートメント
pass
は実際には null
ステートメントであり、一般にプレースホルダーとして使用されます。関数またはループを宣言したいが実装を提供したくない場合、pass
ステートメントを使用できます。C プログラミング言語の ;
またはアセンブリ言語の nop
に似ています。
pass
ステートメントは、pass
が実行されても何も起こらないことを意味する操作なし(NOP)になります。
では、両方に何も起こらない場合の pass
と Python コメントの違いは何ですか?
コメントは無視され、実行されませんが、pass
ステートメントは実行され、何も起こりません。
Python での pass
の構文は次のとおりです。
pass
将来ループまたは関数の実装を提供したい場合、Python では関数またはループが空のボディを持つことはできないため、pass
ステートメントを使用する必要があります。
pass
ステートメントは空のボディを作成します。
pass
ステートメントの例
l = ['p', 'y', 't', 'h', 'o', 'n']
for i in l:
pass
したがって、ここでは、for
ループには pass
ステートメントで示される空のボディがあります。pass
ステートメントがなく、for
の本文が空のままの場合、SyntaxError-expected a indented block
が発生します。
同様に、クラスと関数を将来実装する予定の場合、クラスと関数で pass
を使用することもできます。以下の例を検討してください。
def function(args):
pass
class ABC:
pass
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn