简体   繁体   中英

Which of `if x:` or `if x != 0:` is preferred in Python?

Assuming that x is an integer, the construct if x: is functionally the same as if x != 0: in Python. Some languages' style guides explicitly forbid against the former -- for example, ActionScript/Flex's style guide states that you should never implicitly cast an int to bool for this sort of thing.

Does Python have a preference? A link to a PEP or other authoritative source would be best.

The construct: if x: is generally used to check against boolean values.

For int s the use of the explicit x != 0 is preferred - along the lines of explicit is better than implicit ( PEP 20 - Zen of Python ).

There's no hard and fast rule here. Here are some examples where I would use each:

Suppose that I'm interfacing to some function that returns -1 on error and 0 on success. Such functions are pretty common in C, and they crop up in Python frequently when using a library that wraps C functions. In that case, I'd use if x: .

On the other hand, if I'm about to divide by x and I want to make sure that x isn't 0 , then I'm going to be explicit and write if x != 0 .

As a rough rule of thumb, if I treat x as a bool throughout a function, then I'm likely to use if x: -- even if I can prove that x will be an int . If in the future I decide I want to pass a bool (or some other type!) to the function, I wouldn't need to modify it.

On the other hand, if I'm genuinely using x like an int , then I'm likely to spell out the 0 .

Typically, I read:

if(x) to be a question about existence.

if( x != 0) to be a question about a number.

It depends on what you want; if x is an integer, they're equivalent, but you should write the code that matches your exact intention.

if x:
    # x is anything that evaluates to a True value
if x != 0:
    # x is anything that is not equal to 0

Might I suggest that the amount of bickering over this question is enough to answer it?

Some argue that it "if x" should only be used for Z, others for Y, others for X.

If such a simple statement is able to create such a fuss, to me it is clear that the statement is not clear enough. Write what you mean.

If you want to check that x is equal to 0, then write "if x == 0". If you want to check if x exists, write "if x is not None".

Then there is no confusion, no arguing, no debate.

If you want to test x in a boolean context:

if x:

More explicit, for x validity (doesn't match empty containers):

if x is not None:

If you want to test strictly in integer context:

if x != 0:

This last one is actually implicitly comparing types.

Wouldn't if x is not 0: be the preferred method in Python, compared to if x != 0: ?

Yes, the former is a bit longer to write, but I was under the impression that is and is not are preferred over == and != . This makes Python easier to read as a natural language than as a programming language.

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