Skip to content

Instantly share code, notes, and snippets.

@etigui
Created March 17, 2019 10:34
Show Gist options
  • Select an option

  • Save etigui/dc879447aa10199708b7096efe4fbeef to your computer and use it in GitHub Desktop.

Select an option

Save etigui/dc879447aa10199708b7096efe4fbeef to your computer and use it in GitHub Desktop.
Loop "for" and "while"can have an "else" branch (PyTricks)
# Python's `for` and `while` loops
# support an `else` clause that executes
# only if the loops terminates without
# hitting a `break` statement.
def contains(haystack, needle):
"""
Throw a ValueError if `needle` not
in `haystack`.
"""
for item in haystack:
if item == needle:
break
else:
# The `else` here is a
# "completion clause" that runs
# only if the loop ran to completion
# without hitting a `break` statement.
raise ValueError('Needle not found')
>>> contains([23, 'needle', 0xbadc0ffee], 'needle')
None
>>> contains([23, 42, 0xbadc0ffee], 'needle')
ValueError: "Needle not found"
# Personally, I'm not a fan of the `else`
# "completion clause" in loops because
# I find it confusing. I'd rather do
# something like this:
def better_contains(haystack, needle):
for item in haystack:
if item == needle:
return
raise ValueError('Needle not found')
# Note: Typically you'd write something
# like this to do a membership test,
# which is much more Pythonic:
if needle not in haystack:
raise ValueError('Needle not found')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment