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.

3
  • It is not a function. Please read minimal reproducible example for creating a more clear question. Commented Jun 22, 2020 at 21:09
  • 1
    There's nothing special about if __name__ == "__main__":, it is the same if construct as if True: with respect to scope. It is definitely not a function Commented Jun 22, 2020 at 21:10
  • 1
    if block does not have its own context. Does not matter what it's used for. Commented Jun 22, 2020 at 21:10

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

It's declared in the global scope, outside of all function bodies.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.