-
-
Save thomasballinger/3041354 to your computer and use it in GitHub Desktop.
Dynamic Dispatch in Python
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 FlexiDict(dict): | |
def get_gt(self, key): | |
return [v for k,v in d.iteritems() if k > key] | |
def __getattr__(self, name): | |
if name.startswith("find_"): | |
params = name.split("_") | |
if len(params) == 3: | |
def func(): | |
return dict.__getattribute__(self, "get_gt")(int(params[2])) | |
return func | |
else: | |
def func(): | |
return dict.__getattribute__(self, "get")(int(params[1])) | |
return func | |
d = FlexiDict(list(enumerate('abcde'))) | |
print d.find_1() | |
print d.find_gt_2() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment