> dynamically redefine functions even to violate lexical scoping
When would someone want to do this?
When would someone want to do this?
For example:
def foo():
print('usually this value is inaccessible from "python land"')
def extract_printed_values(bar):
global print
old_print, returnv = print, None
def new_print(x):
nonlocal returnv
old_print(returnv := x)
print = new_print
bar()
print = old_print
return returnv
fooval = extract_printed_values(foo)
fooval will then be equal to whatever value foo printed to the console, otherwise foo will behave exactly the same as normal (assuming it only printed a single value), and print will even behave normally afterwards