I wish to do :
if var not in globals():
global var
var = -1
or
try : var
except NameError :
global var
var = -1
The problem is :
global var
^
SyntaxError: name 'var' is used prior to global declaration
So what can be done to achieve similar effect ?
EDIT :
This is not at module level, but rather in a function. The variable is supposed to store a position value that the function uses and updates in it but needs to persist between calls.
I have been made aware that the above code is bad practise, and I agree, and request those agreeing to suggest alternative "good practise" methods, if possible.
EDIT 2 :
This question is required to help me fix my previous question.
There, the obj_cnt
is getting reset at every call, bugging up the function that accesses objects from a pickled file by an "index" (an abstraction), as I try to avoid unnecessary seeking and reading by querying indexes in sorted order.
The above should clear up the "Why?" part.
EDIT 3:
I did not end up needing to do this convoluted stuff. I solved the above question by using classes.
Nevertheless, thanks to everyone who helped me here.