3

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.

peter.petrov
  • 36,377
  • 12
  • 75
  • 137

2 Answers2

4

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

Jonas Byström
  • 23,783
  • 21
  • 97
  • 141
3
if __name__ == "__main__"

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

Eno Gerguri
  • 633
  • 4
  • 22