# Python CodeRecipe|assertの使い方

Home Python Keywords

# assertの例1

Test if a condition returns True:

    x = "hello"#if condition returns True, then nothing happens:assert x == "hello"#if condition returns 
    False, AssertionError is raised:assert x == "goodbye"

# assertの定義及び使い方

assert関数はデバッグ時に使用されます。 コード。 assert関数を使用すると、 conditionはTrueを返します。 アサーションエラー。 Falseが返された場合は、メッセージを表示することもできます。 次に例を示します。

# assertの例2

Write a message if the condition is False:

    x = "hello"#if condition returns 
    False, AssertionError is raised:assert x == "goodbye", "x 
    should be 'hello'"

Home Python Keywords