Declare variable as global inside block __name__ == "__main__"

Viewed 89

Inside the block

if __name__ == "__main__":

do I need to declare a variable as global in order to set its value?
Or... is this block not a function and hence I don't need to do this?

I guess the latter is true but I want to double-check and make sure I understand this better.

2 Answers

You just need to declare the variable, global keyword only makes sense in a function.

if __name__ == "__main__"

This if statement does not have its own context, therefore variables can be modified without the need for the global keyword.

Related