Skip to content Skip to sidebar Skip to footer

What Does This "if" Statement Mean?

In an example Hadoop reduce program, such an 'if' statement exists. I am wondering what it means when the same variable 'a' is used twice in the 'if' statement, as follows if a and

Solution 1:

It will test for equality (a == b) only if a is truthy.

It can be rewritten as

if a:
    if a == b:
        print("It is working!")

Note the operator precedence: it's not equal to if (a and a) == b.

Solution 2:

There's no real magic here.

And and expression is truthy iff both sides are truthy.

a is truthy iff it's not False, None, a numeric 0, or an empty collection.

a == b is truthy iff a and b are equal (in some sense appropriate to their type).

So, for example, if a and b can be either a list or None, this will be true if a is a non-empty list and b is a non-empty list with the same values.

Post a Comment for "What Does This "if" Statement Mean?"