why won't slicing lists raise IndexError?
Rick Johnson
rantingrickjohnson at gmail.com
Fri Dec 8 08:12:35 EST 2017
Python wrote:
[...]
>
> In this snippet (which again, we agreed was an incomplete
> academic example):
>
> if item:
> process(item)
> else:
> do_without_item()
>
> Can you please explain to me what sort of Python
> syntactical construct do_without_item() could be, other
> than a call to a Python callable object (i.e. a function)?
> Then, could you explain to me how that particular
> syntactical construct is in any way equivalent to the pass
> statement? Could you then explain to me how using that in
> the simple example given makes any sense whatsoever?
I have already backed my argument with multiple code
examples, exhaustive explanations, and even constructed a
metaphor that was slightly entertaining. Yet, none of those
are good enough? Alas, allow me to try once more.
Consider:
> if item:
> process(item)
> else:
> do_without_item()
The above code sample is semantically equivalent to the
following real-life "situational logic":
[man sits on couch and enjoys sports programming]
if thirsty:
grab_beer_from_fridge()
else:
stay_on_couch()
Above, just as with Terry's code, the else-clause cannot
justify itself. And why you ask? Because, we were _already_
on the damned couch! The else-clause is a "do nothing"
condition, hence, it is functionally equivalent to:
else:
pass
*HENCE*, it is super-freaking-fluous! Get it?
However. Here is an example which _can_ justify the else-
clause
if thirsty and (not tooLazyToMove):
grab_beer_from_fridge()
else:
tell_wife_to_grab_beer()
Heck, even tangential actions can be justified. Observe:
import random
if thirsty and (not tooLazyToMove):
grab_beer_from_fridge()
else:
random.choice([
scratch_self,
eat_tater_chip,
change_channel,
relieve_abdominal_pressure,
])()
More information about the Python-list
mailing list