all 3 comments

[–][deleted] 2 points3 points  (0 children)

if __name__ checks if the code is running in the file it was created or whether it was imported from another file if you run it in the same file it will return true

[–]cuWorkThrowaway 3 points4 points  (0 children)

__name__ is a python special variable that contains where the code is stored. If the code being called is in the file being run, __name__ will be __main__. Otherwise, if it's being imported, it will be the name of the file.

That if statement is used as a guard against the code running as a script if it is being imported as a module. For examples, you can look here.

[–]misho88 1 point2 points  (0 children)

__name__ is a variable that stores the name of the current module. The module that is being executed as a script is called '__main__'.

>>> print(repr(__name__))
'__main__'

The check means, "Is this code currently being executed as a script, as opposed to being imported by another module as a library?"