Created
June 15, 2017 09:39
-
-
Save nopcall/21cdd52ae395bac974f5118f04c5e89a to your computer and use it in GitHub Desktop.
dispather
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
#!/bin/env python3 | |
# -*- coding: utf-8 -*- | |
class dispatch: | |
class _handler: | |
def __init__(self): | |
self._handleTale = {} | |
def register(self, argType): | |
self._handleTale.setdefault(argType, None) | |
def wrapper(handleFunc): | |
self._handleTale[argType] = handleFunc | |
return wrapper | |
def __call__(self, arg): | |
argType = type(arg) | |
return self._handleTale[argType](arg) | |
def __call__(self, method): | |
method = self._handler() | |
return method | |
@dispatch() | |
def handler(arg): | |
print(arg) | |
@handler.register(str) | |
def _(arg:str): | |
print(arg) | |
@handler.register(int) | |
def _(arg:int): | |
print(arg) | |
handler("emacs") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment