else
/ elif
used after return
PYL-R1705224 return False
225
226 target_item = list(wd_data).pop()
227 try:228 item.mergeInto(target_item)
229 except APIError:
230 # warning already printed by the API
145 pywikibot.info(
146 f'Adding {coordinate.lat}, {coordinate.lon} to {item.title()}')
147 # todo: handle exceptions using self.user_add_claim
148 try:149 item.addClaim(newclaim)
150 except CoordinateGlobeUnknownError as e:
151 pywikibot.info(f'Skipping unsupported globe: {e.args}')
1147 if template in self.hiddentemplates:
1148 # if the whitetemplate is not in the images description, we don't
1149 # care
1150 try:1151 self.all_licenses.remove(template)
1152 except ValueError:
1153 return False
312 .. versionchanged:: 7.1
313 Previous implementation only checked for cached keys.
314 """
315 try:316 self[key]
317 except KeyError:
318 return False
299
300 .. versionadded:: 7.1
301 """
302 try:303 self._get_cached(key)
304 except KeyError:
305 return False
The use of else
or elif
becomes redundant and can be dropped if the last statement under the leading if
/ elif
block is a return
statement.
In the case of an elif
after return
, it can be written as a separate if
block.
For else
blocks after return
, 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):
if x % 2 == 0:
return 'Even'
else:
return 'Odd'
def what_is_this_number(x):
if x % 2 == 0 and x >= 0:
return 'Even'
elif x % 2 == 0 and x < 0:
return 'Even and Negative'
elif x % 2 != 0 and x < 0:
return 'Odd and Negative.'
else:
return 'Odd'
def classify_number(x):
if x % 2 == 0:
return 'Even'
return 'Odd'
def what_is_this_number(x):
if x % 2 == 0 and x >= 0:
return 'Even'
if x % 2 == 0 and x < 0:
return 'Even and Negative'
if x % 2 != 0 and x < 0:
return 'Odd and Negative'
return 'Odd'