why won't slicing lists raise IndexError?
Rick Johnson
rantingrickjohnson at gmail.com
Tue Dec 5 21:23:04 EST 2017
Steve D'Aprano wrote:
[...]
> You've already been told that there's no indication or
> reason to believe that it is a non-action. You've already
> been given at least one possible action. It isn't a non-
> action, it is two distinct actions:
>
> - the action you take when the slice is non-empty;
>
> - the action you take when the slice is empty.
When Python follows a logic clause like a train skating
along a set of railroad tracks, and finds itself in a *GHOST
TOWN*, that's not an action -- "Steve-o" -- it's a non-
action.
Listen, I think it might help you to understand the
absurdity of this else-clause if you ruminate about this
code using a metaphor. Below, i've defined some fictional
characters and objects that will live in a little
metaphorical world, and futhermore, explained how these
characters and objects relate to our real-life code example.
<METAPHOR class="Semantics101"
title="A ghost town named Elseville"
onload="spread_enlightenment_to(head);"
style="sarcasm:50%;weight:heavy;"
onunload="pwnd('day-app-ran-o');"
>
TERRY("THE BUILDER"): Terry is a personable fella who builds
little shanty towns for a living.
IF_VILLE: This is the first of two towns that Terry
establish in our little metaphorical world. And although
it's not quite the shining-city-on-a-hill that he, or the
inhabitants, had envisioned, he did manage to build a proper
train platform and a small warehouse for strong
deliverables, so we shouldn't be too critical of his work
here.
ELSE_VILLE: This is the "other" town that Terry establish.
However, after slaving away in the hot desert sun building
IF_VILLE all day, Terry decided to go home and drink a case
of beer, became drunk, and then forgot all about his
responsibilities to develope ELSE_VILLE. This has happened
before. But we typically forgive Terry for his irresponsible
nature simple because he's such a personable fella. However,
if this gets any worse, we may have to give him an
intervention.
KENNY_LINTER: Kenny Linter is a faithful civil servant who's
sole job is to inspect little shanty towns. And though he's
a little short on manners, he is typically competent
_enough_ to tell us if our towns are built to quality
standards, and if they're not built to quality standards,
well, it's his job to annoy us until we repair them.
Unfortunately, i've got some bad news to share with you.
After hastily exiting the train in ELSE-VILLE last night and
not realizing there was no platform in this town (Thanks
Terry!), KENNY_LINTER has broken both of his legs, fractured
an arm, and scratched his face up pretty bad. So i doubt
we'll be hearing from him in this episode, but stay tuned
for future appearances.
THE_TRAIN: The Train represents our Python script.
THE_TRACKS: The (railroad)tracks represent our program
logic. And the train's course is bound to these tracks
THE_ENGINEER: This is the Python runtime which "drives" the
train.
THE_TRACK_SWITCH: The Track Switch is a mechanical section
of the track (aka: logic gate) placed approximately halfway
between the train station and the two shanty towns that
Terry built. The switch allows the train to travel in one of
two directions -- one leading to IF_VILLE, and one leading
to ELSE_VILLE. Now, the mechanical apparatus of the switch
is spring loaded, and thus, by default, it always sends a
passing train to ELSE_VILLE. However, there is a tiny
control button mounted on a nearby fence post, one which
when pressed, will align the tracks with IF_VILLE. However,
since the trains in this metaphor have no brakes, and since
the button is really ~really~ small -- and since i couldn't
think of a more creative scenario! -- there is only one
creature who can press this button (TRUTHY_CLAWS!). And she
presses this button using one of her long pointy claws, and
thus, can send the train towards IF_VILLE.
TRUTHY_CLAWS: TruthyClaws (or "TC", as we like to call her)
is a mostly harmless anthropomorphized version of a
marsupial who's long claws only seem useful (at least upon
first sight) for causing a dreadful fright. But in reality,
these claws serve a vital purpose in our little metaphorical
world. You see, of ~all~ the characters in our little
universe, only TC (using one of her dreadfully long claws)
can press the little button on the TRACK_SWITCH and send us
along a path to IF-VILLE. And, every time TC takes a ride
with us in the train, she presses the little button for us,
and off we go to IF-VILLE (Hooray!). However, sometimes us
guys get a little rowdy and tell dirty jokes during the
trip, and TC, being uptight and all, tends to get offended,
and sometimes she refuses to ride with us. So, whenever TC
is with us, we always go to IF-VILLE, but if she's off
pouting somewhere, the train goes to ELSE-VILLE
THE_TRACK_DEVIL: The Track Devil is a supernatural being in
the mold of Loki who specializes in all forms of mischievous
pranks. And you never know where the little devil might pop-
up next. Rumor has it he's been seen lurking around the
THE_TRACK_SWITCH and placing coinage on the tracks, but we
can neither confirm nor deny these rumours. Although, there
is a strange old woman who lives on the outskirts of
ELSE_VILLE who claims she can foretell the appearance of the
track devil. But most people 'round these parts consider her
to be a total nut job.
</METAPHOR>
Using this metaphor we can understand that the else-clause in
Terry's example serves no practical purpose. And being that
Kenny Linter is already in the hospital for an injury he
would have _never_ suffered if the undeveloped ELSE_VILLE
had not been on his inspection roster, how the hell can you
justify your argument? I mean, seriously. Terry almost
killed Kenny!
More information about the Python-list
mailing list