If or elif or true, then do something

it's just academic interest. I meet the following situation.

either_true = False if x: ...do something1 either_true = True elif y: ...do something2 either_true = True if either_true: ..do something3 

is there any pythonic way to do this, or is there a better way to program in general. Basically, something3 is only executed if true or true.

+44
python
Feb 13 '14 at 2:04
source share
10 answers

Your code is almost optimal when it comes to code repetition and evaluation. The only thing I can think of to avoid repetition would be:

 # be optimistic! either_true = True if x: do_something1 elif y: do_something2 else: either_true = False if either_true: do_something3 

This removes one assignment, although the total number of rows does not change.

The advantage is that this works with conditions n without adding any other assignments, while the current solution requires either_true = True for each condition.

In my opinion, they have the same degree of readability, but the code above will be better with a lot of conditions.

There is also no “pythonic” method other than the readable one, which avoids code repetition and is optimal in terms of efficiency, and I don’t know of any “better programming” to achieve the same result.

+31
Feb 13
source share

You can also completely omit either_true flag if doSomething3 is a single line of code (e.g. calling a function):

 if x: ..do something 1 ..do something 3 elif y: ..do something 2 ..do something 3 

It retains the nice property of evaluating x and y no more than once (and y will not be evaluated if x true).

+35
Feb 13 '14 at 14:12
source share

I would handle this using nested if.e statements.

 if x or y: if x: ...do something1 elif y: ...do something2 ...do something3 

As noted in some comments, the best solution will depend on what x and y are. If you are comfortable with easy readability / compressed code, then these or other answers should be accurate. If, however, x and y were expensive function calls, then it would be better to do something more like what you did to avoid calling the function twice.

+19
Feb 13 '14 at
source share

You can transfer part of it to a function:

 def do_stuff(): if x: ...do something1 return True elif y: ...do something2 return True else: return False if do_stuff(): ..do something3 

Or all this in a function:

 def do_stuff() if x: ...do something1 elif y: ...do something2 else: return ..do something3 
+7
Feb 13 '14 at 14:11
source share

In the spirit of offering a completely different solution to those already offered, you can create a list of structured dictionaries that allows you to set up several cases related to your predefined "somethings"

 cases = [ {'condition' : x, 'action' : something1}, {'condition' : not x and y, 'action' : something2}, {'condition' : x or y, 'action' : something3}, ] for c in cases: if c['condition']: c['action'] 

I really like this method (and I just opened it, trying to come up with a unique answer to this question, thanks!) - it’s really obvious which case is connected with which action, and it’s easy to add a few more cases without adding more if / statements else.

+6
Feb 13 '14 at 17:45
source share
 if x or y: dosomethig1() if x else dosomething2() dosomething3() 

Of course, this evaluates x.__nonzero__ . This is usually not very important, but if it is expensive, you can always evaluate it and save it in a temporary variable.

+5
Feb 13 '14 at 2:08
source share

For all these suggestions and any others you come up with, note that if x and y are expensive expressions:

 if askTheServer() or readTheOneGigabyteConfigFile(): ... 

you can assign values ​​that these expressions return to quick calculations:

 x = askTheServer() y = readTheOneGigabyteConfigFile() if x or y : ... 
+4
Feb 14 '14 at 0:37
source share
 either_true = x or y if x: ...do something1 elif y: ...do something2 if either_true: ..do something3 
+2
Feb 13 '14 at 14:14
source share

I would wrap the functions..do in the function and write one if-elif:

 def do_x(): .. do something 1 .. do something 3 def do_y(): .. do something 2 .. do something 3 if x: do_x() elif y: do_y() 

It is nice if .. something has to do with a lot of things.

+2
Feb 13 '14 at 23:17
source share

if something is rather short, like do (1), do (2) or something like that, you can simply do it like this:

 (x and (do(1), x) or y and (do(2), y)) and do(3) 
0
Feb 21 '14 at 6:57
source share



All Articles