After stumbling upon this question I've been playing around with exec() to better understand how it works. I'm trying to get this abomination of a script to increment a global variable, read itself, and call exec with the new global value until the recursion limit is met. The best I've been able to come up with is having one file declare the global and then call the next file (an exact duplicate minus the variable declaration) which will then recursively call itself. Here's the code for the first one:
# recurse.py
def func():
global x
x += 1
with open('recurse2.py', 'r') as f:
try:
exec(f.read(), {'x': x})
except RecursionError:
print('maximum recursion depth reached at', x)
x = 0
func()
And here's the file it executes, that will execute itself:
# recurse2.py
def func():
global x
x += 1
with open('recurse2.py', 'r') as f:
try:
exec(f.read(), {'x': x})
except RecursionError:
print('maximum recursion depth reached at', x)
func()
Is it possible to achieve the same effect with only one file?