I have a file, f.py:
def func():
if 'x' not in globals():
setattr(f, 'x', 0)
global x
x += 10
print x
I am in an interactive session:
>>> import f
>>> f.func()
Traceback (most recent call last):
[...]
NameError: global name 'x' is not defined
How should I modify func to define x if it is not already defined?
1 Answer