def replacingGlobals(function, **newGlobals):
 try:
  codeObject = function.func_code
  funcGlobals = function.func_globals
 except AttributeError:
  codeObject = function.__code__
  funcGlobals = function.__globals__
 for key in newGlobals:
  if (key not in funcGlobals):
   raise TypeError('Name bound by replacingGlobals but not present in module: {}'.format(key))
 mergedGlobals = {}
 mergedGlobals.update(funcGlobals)
 mergedGlobals.update(newGlobals)
 newFunction = FunctionType(codeObject, mergedGlobals)
 mergedGlobals[function.__name__] = newFunction
 return newFunction

def replacingGlobals ( function , ** newGlobals ) : try : codeObject = function . func_code funcGlobals = function . func_globals except AttributeError : codeObject = function . __code__ funcGlobals = function . __globals__ for key in newGlobals : if ( key not in funcGlobals ) : raise TypeError ( 'Name bound by replacingGlobals but not present in module: {}' . format ( key ) ) mergedGlobals = { } mergedGlobals . update ( funcGlobals ) mergedGlobals . update ( newGlobals ) newFunction = FunctionType ( codeObject , mergedGlobals ) mergedGlobals [ function . __name__ ] = newFunction return newFunction
create a copy of the given function with the given globals substituted .