elif
/ else
block after continue
PYL-R172412 else:
13 new = None
14 for i, (tok, val) in enumerate(list(pattern.data)):
15 if not found and tok == sre_parse.LITERAL and val == ord(at):16 found = True
17 del pattern[i]
18 continue
19 prev_val = None
20 for i, (tok, val) in enumerate(list(seq)):
21 if tok == prev_tok and (val == prev_val or _val_eq(val, prev_val)):
22 if tok == MAX_REPEAT: 23 min_repeat = min(MAXREPEAT, prev_val[0] + val[0])
24 max_repeat = min(MAXREPEAT, prev_val[1] + val[1])
25 prev_val = min_repeat, max_repeat, prev_val[2]
The use of else
or elif
becomes redundant and can be dropped if the last statement under the leading if
/ elif
block is a continue
statement.
In the case of an elif
after continue
, it can be written as a separate if
block.
For else
blocks after continue
, the statements can be shifted out of else
. Please refer to the examples below for reference.
Refactoring the code this way can improve code-readability and make it easier to maintain.
def classify_number(x):
for num in range(x):
if x % 2 == 0:
continue
else:
print(f"{num} is Odd}")
def classify_number(x):
for num in range(x):
if x % 2 == 0:
continue
print(f"{num} is Odd}")