Given this python script:
def s():
try:
time.sleep(1)
except NameError:
import time
s()
for i in range(10):
s()
print(i)
This gives me a RecursionError, but I don't see why because I would expect it to:
s
function0
I cannot see why it would call the s()
function recursively more than 2 times.
For clarification purposes:
Since the import
is inside a function, names it defines are local to that function's scope by default. To make it global, use global time
.
def s():
global time
try:
time.sleep(1)
except NameError:
import time
s()