Created
October 29, 2012 04:37
-
-
Save yamahigashi/3971547 to your computer and use it in GitHub Desktop.
関数の動的生成
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
def hoehoee(arg): | |
''' なんか実装してね ''' | |
return arg | |
#------------------------------------------------------------------------# | |
#------------------- 登録時の補助 ---------------------------# | |
#------------------------------------------------------------------------# | |
def _make_func_name(arg): | |
''' 関数名生成良い感じに細工する 禁則文字抜くとか置換するといいと思う ''' | |
return 'generatetd_func_' + arg.lower().replace(" ", "") | |
def _make_command_callback(arg): | |
''' 関数オブジェクトを作って返す | |
CAUTION: arg may not contain white space | |
''' | |
func_name = _make_func_name(arg) + '_Execute' | |
def event_handler(): | |
return hoehoee(arg) | |
event_handler.__name__ = func_name | |
return event_handler | |
#------------------------------------------------------------------------# | |
#------------------- グローバルに登録するよ ---------------------------# | |
#------------------------------------------------------------------------# | |
for hoge in SOME_LIST: | |
callback = _make_command_callback(hoge) | |
globals()[callback.__name__] = callback |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment