Python: avoid if condition?

What's better?

if not var: var = get_var() 
(or)

var = var or get_var ()

Also, how do you know which is better than two?
edit:
Another option from steve,

 var = var if var else get_var() 
+4
source share
9 answers

The better you like it. I would use the first version with if , but this is very personal.

+7
source

If the two styles differ in such a stylish style, I use timeit as a tie-breaker: it should be closer to the main body of Python faster, i.e. better. Hey, this is better than endless debate, y? -) So:

 $ python -mtimeit -s'var=0; getvar=lambda:0' 'var = var or getvar()' 1000000 loops, best of 3: 0.359 usec per loop $ python -mtimeit -s'var=0; getvar=lambda:0' 'if not var: var = getvar()' 1000000 loops, best of 3: 0.361 usec per loop $ python -mtimeit -s'var=1; getvar=lambda:1' 'var = var or getvar()' 10000000 loops, best of 3: 0.123 usec per loop $ python -mtimeit -s'var=1; getvar=lambda:1' 'if not var: var = getvar()' 10000000 loops, best of 3: 0.0899 usec per loop 

the if has an equivalent if var is false, faster when it is true .

+7
source

In fact, if you are trying to determine if the var parameter was previously set using the get_var call, I would argue that both forms are wrong . Python treats a number of completely ordinary values โ€‹โ€‹as an estimate of the logical "false": 0, None, [], (,), set (), and {}. So, let var be an integer, and get_var () will return 0. Now, no matter what form you use, get_var () will be called again and again, although we already know that var is 0!

There are several ways to determine if a variable has been defined:

  • look at the dict returned by globals () or locals ()

  • wrap the var = var statement in a try / except block, capture on NameError

  • use a sentinel value, such as None, and initialize var for that value; then you can check if var is None: var = get_var() (using 'is', not '=='). If you're out of luck, and None is the potential value that should be returned from get_var (), then you will need to define your own special, not yet defined value, using something like NOT_DEFINED = object() , initialize var with it, and then you can check if var is NOT_DEFINED .

+4
source

The first version is more intuitive for me. But then again, it's all about your own taste.

+2
source

For me, the first idiom using an explicit if is preferable because it is more explicit.

However, I saw that or is referred to as the preferred / more pythonic.

So, on the one hand, Explicit is better than implicit (quoting Zen), on the other hand, a short expression can be considered as a Python expression (although it is shorter in all cases, it is not equivalent to pythonic!)

A close SO question is what is the most idiomatic way of converting None to an empty string , and both if and idioms were indicated there.

+2
source

The second is more Pythonic, and I usually use this.

+1
source

Wrong.

The first is understandable to someone who reads the code.

Ask yourself if, when you think about the problem you are trying to solve, the concepts that come to mind are closer to "if it is, then this" or "a logical OR of two almost, but not quite, logical variables."

+1
source

Both will eventually be compiled for the same code, given that this is a short circuit operator (it does not evaluate the argument of the right hand if the left hand is true).

I would prefer the first, as it more clearly expresses your intentions. The second is probably more compact.

+1
source

The interpreter is likely to execute both of them. There are trade-offs regarding code readability. Which statement is easier to recognize that he is doing? Of course, the first is much clearer. For me, after reading this post, I had to think more about the second. I would use the first because of its increased readability, even if the second statement is a bit โ€œsexierโ€.

Hope this helps.
-tjw

+1
source

Source: https://habr.com/ru/post/1303764/


All Articles