def namespaced_function(function, global_dict, defaults=None, preserve_context=False):
 if (defaults is None):
  defaults = function.__defaults__
 if preserve_context:
  _global_dict = function.__globals__.copy()
  _global_dict.update(global_dict)
  global_dict = _global_dict
 new_namespaced_function = types.FunctionType(function.__code__, global_dict, name=function.__name__, argdefs=defaults)
 new_namespaced_function.__dict__.update(function.__dict__)
 return new_namespaced_function

def namespaced_function ( function , global_dict , defaults = None , preserve_context = False ) : if ( defaults is None ) : defaults = function . __defaults__ if preserve_context : _global_dict = function . __globals__ . copy ( ) _global_dict . update ( global_dict ) global_dict = _global_dict new_namespaced_function = types . FunctionType ( function . __code__ , global_dict , name = function . __name__ , argdefs = defaults ) new_namespaced_function . __dict__ . update ( function . __dict__ ) return new_namespaced_function
redefine a function under a different globals() namespace scope preserve_context: allow to keep the context taken from orignal namespace .