Skip to content

Instantly share code, notes, and snippets.

@yamahigashi
Created October 29, 2012 04:37
Show Gist options
  • Save yamahigashi/3971547 to your computer and use it in GitHub Desktop.
Save yamahigashi/3971547 to your computer and use it in GitHub Desktop.
関数の動的生成
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