简体   繁体   中英

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 a == b:
    print "It is working!"

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM