elif
/ else
block after continue
PYL-R1724 76try:
77 lg.info('Reading backfiles.txt...')
78 for line in open(config_variables["backfiles"]):
79 if list(line)[0] == '#': 80 continue
81 else:
82 lg.debug(line)
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}")