Skip to content

Instantly share code, notes, and snippets.

@uselesslemma
Last active August 11, 2020 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uselesslemma/be75f1a05d849f92a1d8bbeed0efa5de to your computer and use it in GitHub Desktop.
Save uselesslemma/be75f1a05d849f92a1d8bbeed0efa5de to your computer and use it in GitHub Desktop.
Semi-regular Python case studies! Inspired by @dbader.
# Because Python has first-class functions, they
# can be used to emulate switch/case statements!
def switch_if(operator, a, b):
if operator == 'add':
return a + b
elif operator == 'sub':
return a - b
elif operator == 'mul':
return a * b
elif operator == 'div':
return a / b
else:
return None
def switch_dict(operator, a, b):
return {
'add': lambda: a + b,
'sub': lambda: a - b,
'mul': lambda: a * b,
'div': lambda: a / b,
}.get(operator, lambda: None)()
switch_if('add', 7, 4)
# Output: 11
switch_dict('add', 7, 4)
# Output: 11
switch_if('mul', 7, 4)
# Output: 28
switch_dict('mul', 7, 4)
# Output: 28
switch_if('Papa Bless', 7, 4)
# Output: None
switch_dict('Papa Bless', 7, 4)
# Output: None
# Python has a HTTP server built into the
# standard library. This is super handy for
# previewing websites.
# Python 3.x
$ python3 -m http.server
# Python 2.x
$ python -m SimpleHTTPServer 8000
# (This will serve the current directory at
# http://localhost:8000)
# Python Riddle: What will this expression evaluate to?
{True: 'yes', 1: 'no', 1.0: 'maybe'}
# ----------------------------------
# Equivalent expression, step-by-step
xs = dict()
xs[True] = 'yes'
xs[1] = 'no'
xs[1.0] = 'maybe'
# Python considers all dictionary keys used in this example to be equal!
True == 1 == 1.0
# Output: True
"""
Okay, but wait a minute here. I’m sure you can intuitively accept that 1.0 == 1, but why would True be considered equal to 1 as well? The first time I saw this dictionary expression it really stumped me.
After doing some digging in the Python documentation, I learned that Python treats bool as a subclass of int. This is the case in Python 2 and Python 3:
“The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings ‘False’ or ‘True’ are returned, respectively.” (Source)
And yes, this means you can technically use bools as indexes into a list or tuple in Python:
"""
['no', 'yes'][True]
# Output: 'yes'
"""
As far as Python is concerned, True, 1, and 1.0 all represent the same dictionary key. As the interpreter evaluates the dictionary expression, it repeatedly overwrites the value for the key True. This explains why, in the end, the resulting dictionary only contains a single key.
"""
ys = {1.0: 'no'}
ys[True] = 'yes'
ys
# Output: {1.0: 'yes'}
# TODO: Finish this lesson...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment