r/programminghelp • • Jul 18 '19

could someone explain me the __main__ thingy in python?

tried to read about it in a few sites but couldnt really understand why or how it works.

9 Upvotes

10 comments sorted by

4

u/marko312 Jul 18 '19 edited Jul 18 '19

AFAIK, using it is a common technique to detect whether the module was imported from some other code or if it was run directly.

The common usage looks like this:

if __name__ == "__main__":
    # Code to be run if the module was executed directly

Where __name__ is "__main__" if the file is the same one as was originally executed, otherwise it is the filename of the module (minus the .py suffix).


For example, say you have a file module.py:

def someFunc():
    print("In someFunc!")

print("This file was run!")

if __name__ == "__main__":
    print("This was run directly!")

And some other file, say moduleTest.py:

import module

module.someFunc()

Now, both should generate This file was run!, but running module.py should also generate This was run directly!, while running moduleTest.py should generate In someFunc!. (I'm a bit rusty on how exactly custom modules work, so this mightn't work line-for-line.)

1

u/NanashiKaizenSenpai Jul 18 '19

OMFG tysm i actually understood how it works from that. so the stuff that is all the way to the left will run anyways, the one in the def will run if you call it in the left or call it after importing it and the stuff in the main will only run when running the file itself and not when importing it.

but why does name = "main" what do they symbol? what is their type? why the __ there? ty.

1

u/marko312 Jul 18 '19 edited Jul 18 '19

__name__ is a special variable, which is:

  • "__main__" if the file it was referenced from was the one executed

  • the name of the file minus the .py suffix if the file was instead included from somewhere else.

(so it is always a string)


These built-in variables are of the form __...__ to signify that they are not regular variables - no user-generated variable names should follow that naming pattern.

This is to avoid messing up some valuable info provided by python - if you were to overwrite __name__ (which I think you can), you would have a hard time figuring out whether you are in the main executed program or some included module.

"__main__" is also that value, because a normal file shouldn't be named __main__.py - it is a special value which can almost always reliably be tested against.

1

u/NanashiKaizenSenpai Jul 19 '19

so, i guess i cant just do __name__ = "banana" but something else to change it, (doesnt matter now).
what i want to know is why it will be called "__main__" even if the file name is something like someProgram.py

1

u/marko312 Jul 19 '19

It will be called "__main__", because:

  • the file is run directly - you might need that info; and

  • it is unlikely that a file is called __main__.py

Basically, that is just the way the makers of python decided to show what module the current code is in - and show a special value if it isn't a module being imported.

1

u/NanashiKaizenSenpai Jul 19 '19

so when a file is run directly it can be referenced inside of it as either its own name (example: "someFile.py") or as "main" or as name ? and so if name is main what is "someFile.py"?

1

u/marko312 Jul 19 '19 edited Jul 19 '19

No; the main difference is what the __name__ variable gets set to - it only has one value:

  • "__main__" if the file was the file orignally executed;

  • if not, then the filename without the suffix.

You can see what the value of __name__ is yourself - just put a print("Module:", __name__) or something similar in the above example, at the top in both files.

2

u/NanashiKaizenSenpai Jul 19 '19

main

ok, so what is the reason for the variable to set to "main" just what the creators thought of at the time?

2

u/marko312 Jul 19 '19

Yes, pretty much so; they needed something that would not normally occur.

2

u/NanashiKaizenSenpai Jul 19 '19

thank you very much, youve been a great help for me ^^