Why is this not possible?
CONSTANT = 1
def main():
if False:
CONSTANT = 0
print(CONSTANT)
main()
Error:
UnboundLocalError: local variable 'CONSTANT' referenced before assignment
Explicit assignment doesn't change anything:
CONSTANT = 1
def main():
CONSTANT = CONSTANT
if False:
CONSTANT = 0
print(CONSTANT)
main()
Only changing the name does the job:
CONSTANT = 1
def main():
constant = CONSTANT
if False:
constant = 0
print(constant)
main()
That's kind of annoying, can I somehow avoid that behaviour?