-
-
Save jeffs/e6c0749335f6cd1bf10044149b6bd156 to your computer and use it in GitHub Desktop.
nested-polymorphism-overload-py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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