Skip to content

Instantly share code, notes, and snippets.

@nopcall
Created June 15, 2017 09:39
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 nopcall/21cdd52ae395bac974f5118f04c5e89a to your computer and use it in GitHub Desktop.
Save nopcall/21cdd52ae395bac974f5118f04c5e89a to your computer and use it in GitHub Desktop.
dispather
#!/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