21
loading...
This website collects cookies to deliver better user experience
var1
and, depending on the boolean result of different evaluations, var1
is processed differently.var1 is int -> process it as an int
var1 is long string -> process the long string
var1 is short string -> process the short string
if-statements
are the most easily grasping strategy, likely used by all novice programmers but also advanced ones (when coding for very straightforward cases 😉).if isinstance(var1, int):
# do some logic here
elif isinstance(var1, str) and len(var1) > 100:
# do other logic here
elif isinstance(var1, str) and len(var1) <= 100:
# do yet another logic here
else:
# everything before was false
# do this other thing
if/elif/else
implementation as the one presented above? if-statements
will become too lengthy and maybe nested as the number of options grows.if clauses
render that code challenging to test. Surely, impossible to test on its own.if-clauses
.if isinstance(var1, int):
process_is_int(var1)
elif isinstance(var1, str) and len(var1) > 100:
process_is_long_string(var1)
elif isinstance(var1, str) and len(var1) <= 100:
process_is_short_string(var1)
else:
do_else_logic(var1)
if is_int(var1):
process_is_int(var1)
elif is_long_string(var1):
process_is_long_string(var1)
elif is_short_string(var1):
process_is_short_string(var1)
else:
do_else_logic(var1)
is_int
, is_long_string
, and is_short_string
) anywhere else in the code and test them properly. Also, suppose you want to add additional features to those functions in a later stage, for example, new assertions or logging. In that case, you can do it easily without the need to modify the if/elif
block.if-else
blocks and that crazy dictionary strategy I told you about but didn't explain 😉.for
loop. Yes, a for-loop. How?for
loop will loop through the list and operate on the first function that evaluates to True
.# note how our logic is getting defined in separate functions
# as we evolve from if-statements to this pipeline-like method
options = [
# (if_this_is_true, do_this).
(is_int, process_is_int),
(is_long_string, process_is_long_string),
(is_short_string, process_is_short_string),
(lambda x: True, process_the_else_logic),
]
for evaluation, function in options:
if evaluation(var1):
function(var1)
# the loop does not need to continue
# after a function is executed
break
else
logic does not receive var1
as an argument, you can move it to the else
method of the for-loop:options = [
(is_int, process_is_int),
(is_long_string, process_is_long_string),
(is_short_string, process_is_short_string),
]
for evaluation, function in options:
if evaluation(var1):
function(var1)
break
else:
process_the_else_logic()
False
, so there are no losses against the if-else
block.if-statements
to evaluate the state of multiple variables. And, of course, that is also possible using the for loop strategy presented here. But asserting the state of multiple variables is a more complex case that would require the use of functools.partial
, so we leave that discussion for another post.