Skip to content

Instantly share code, notes, and snippets.

@zhangw
Last active December 17, 2015 09:48
Show Gist options
  • Select an option

  • Save zhangw/5589750 to your computer and use it in GitHub Desktop.

Select an option

Save zhangw/5589750 to your computer and use it in GitHub Desktop.
import types
def create_function(name, args):
def y():
pass
y_code = types.CodeType(args, \
y.func_code.co_nlocals, \
y.func_code.co_stacksize, \
y.func_code.co_flags, \
y.func_code.co_code, \
y.func_code.co_consts, \
y.func_code.co_names, \
y.func_code.co_varnames, \
y.func_code.co_filename, \
name, \
y.func_code.co_firstlineno, \
y.func_code.co_lnotab)
return types.FunctionType(y_code, y.func_globals, name)
myfunc = create_function('myfunc', 3)
print repr(myfunc)
print myfunc.func_name
print myfunc.func_code.co_argcount
myfunc(1,2,3,4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment