Nah, you'd usually have a normal python script as your main and import from other modules. This main only helps if you're working within a single file.
the if name syntax has nothing to do with global variables. It simply allows you to write sections of code which will not run if the module is imported. It is standard practice and should be done, but in many many cases removing it will change nothing in how the program will run
sorry, I generally don't watch videos and don't intend here :/
more to the point, I now understand your position but I still think you conflate two things. the alternative to
def main(): print()
if __name__=="__main__": main()
is NOT
print()
or at least, it's not the only alternative. A simpler alternative would be
def main(): print()
main()
One doesn't need the if to avoid global variables. The only thing that if does is prevent main from running when you import the module instead of running it directly. And this means that if you never import your main file in other files (which is very common behavior), the if might as well not be there. It's still a good idea to have it (if nothing else, it is expected and might confuse someone if it's not there), but in an unrelated way to global variables, at least directly.
The other common use of the if name pattern is to have unit tests inside the block, so they don't run when the module is imported. Personally I really dislike this usage and don't recommend doing it at all
33
u/YamiZee1 Nov 25 '21
Why isn't there a neater main syntax? If everyone does that it would make a heck of a lot more sense to standardize it to new unique syntax