Skip to content

Instantly share code, notes, and snippets.

@jeffs

jeffs/switch.py Secret

Last active August 23, 2021 04:27
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 jeffs/e6c0749335f6cd1bf10044149b6bd156 to your computer and use it in GitHub Desktop.
Save jeffs/e6c0749335f6cd1bf10044149b6bd156 to your computer and use it in GitHub Desktop.
nested-polymorphism-overload-py
class Ball:
pass
class Game:
pass
class Tantrum:
pass
class Athlete:
def throw(self, thing):
kind = type(thing)
if kind == Ball:
return "⚾"
elif kind == Game:
return "🤞"
elif kind == Tantrum:
return "👿"
else:
raise Exception(f"don't know how to throw thing of type {kind}")
from switch import *
def test_athlete_throw():
athlete = Athlete()
assert "⚾" == athlete.throw(Ball())
assert "🤞" == athlete.throw(Game())
assert "👿" == athlete.throw(Tantrum())
try:
athlete.throw(42)
assert False, "expected exception"
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment