Python: elif or new if?

后端 未结 5 742
谎友^
谎友^ 2021-01-04 21:52

What is better to use:

if var in X:
    #do_whatever
elif (var in Y):
    #do_whatever2

or:

if var in X:
    #do_whatever
i         


        
5条回答
  •  萌比男神i
    2021-01-04 22:42

    If the second statement is impossible if the first one is true (as is the case here, since you said var can't be in both X and Y), then you should be using an elif. Otherwise, the second check will still run, which is a waste of system resources if you know it's going to false.

提交回复
热议问题