Last active
May 2, 2021 21:30
-
-
Save raeq/fbc0762adeae4a7243d2ecd68456e022 to your computer and use it in GitHub Desktop.
method_overloading
This file contains hidden or 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 functools import singledispatchmethod | |
class Dog: | |
@singledispatchmethod | |
def bark(self, quality): | |
raise NotImplementedError | |
@bark.register | |
def _bark_with_int(self, quality: int): | |
return "BARK " * quality | |
@bark.register | |
def _bark_with_str(self, quality: str): | |
if "loud" in quality.lower(): | |
return "LOUD BARK" | |
if "quiet" in quality.lower(): | |
return "quiet bark" | |
return "A Bark" | |
print(Dog().bark(5)) | |
print(Dog().bark("quiet")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment