Take this example:
def f():
myvar = None
def g():
print myvar
...
myvar = get_real_value()
g()
Is “myvar = None” a conventional (or at least, reasonable) way of declaring the variable, to make it visible to g()? Is there a better way? (Python 2.6.x, if relevant)
There is no need to declare the variable before the definition for
g():However, if you can avoid referencing non-local variables in
g()that would be preferable, which you would probably do here by havingmyvarbe a parameter tog().