I\'d like to make a copy of an entire namespace while replacing some functions with dynamically constructed versions.
In other words, starting with namespace (impo
To my mind, you can do this easily:
import imp, string
st = imp.load_module('st', *imp.find_module('string')) # copy the module
def my_upper(a):
return "a" + a
def my_lower(a):
return a + "a"
st.upper = my_upper
st.lower = my_lower
print string.upper("hello") # HELLO
print string.lower("hello") # hello
print st.upper("hello") # ahello
print st.lower("hello") # helloa
And when you call st.upper("hello")
, it will result in "hello"
.
So, you don't really need to mess with globals.