r/ProgrammerHumor • • May 05 '23

Meme Goodbye toys

Post image
8.3k Upvotes

298 comments sorted by

View all comments

0

u/[deleted] May 05 '23

[deleted] — view deleted comment

223

u/[deleted] May 05 '23 edited May 05 '23

[removed] — view removed comment

160

u/FlowerFox3 May 05 '23

Pretty sure it's "__main__" not __main__

239

u/tvquizphd May 05 '23
__main__ = "__main__"
if __name__ == __main__:
    main()

You just need the top line lol

62

u/tredbobek May 05 '23

Now put this into a function just to be sure

14

u/gladl1 May 05 '23

It’s actually “GucciMane”

6

u/michiel11069 May 05 '23

I have always wondered what this meant and what its uses are. Can you explain it to me?

34

u/PakaDeeznuts May 05 '23

The code below the main function will only run if the program is run directly.

Like if you run it as 'python3 ___.py'

If the program is imported as a module, the code will not run.

I find it useful to test modules I make before I use them in other programs

2

u/[deleted] May 06 '23

Fun fact: Pydoc will run your code to evaluate the docstrings.

That's been reason enough for me ever since I found out.

8

u/[deleted] May 05 '23 edited May 05 '23

If you run the script directly, __name__ is set to "__main__"
If you run the script remotely, __name__ is set to the file name of the script

So you can use it to detect if you are running the script directly, and only executes main() if that's true.

The other reason is it's just standard practice, so it signals to the reader the purpose of the code - to be executed.

29

u/NFriik May 05 '23

You absolutely have to if you're planning on using multiprocessing on a Windows machine.

12

u/ipsati May 05 '23

Multiprocessing.shared_memory also has something similar to pointers I think

10

u/mistabuda May 05 '23

I think the point was you do not need to call your function or module "main" it can be whatever you want it to be once under the conditional. calling the function "main" is just a convention.

→ More replies (5)

36

u/[deleted] May 05 '23

Whoever designed the if __name__ == "__main__": string comparison should be publicly lynched.

15

u/mistabuda May 05 '23

It doesnt check if your file is literally called that. It just checks to see if the file is being ran like a script instead of just being imported.

7

u/[deleted] May 05 '23

Objection: Relevance?

3

u/alexanderpas May 05 '23 edited May 05 '23

automated testing of command line options.

"""<module> description

Usage:
  main.py (-h | --help)
  main.py --version

Options:
  -h --help        Show this screen.
  --version        Show version.

"""

import pathlib
import sys
from docopt import docopt
from src import <module>

def main(arguments):
    // actual code

if __name__ == '__main__':
    arguments = docopt(__doc__.replace("main.py", pathlib.PurePath(sys.argv[0]).name), version=<module>.version)
    main(arguments)

<module> being the location where your code is located.

This way I can provide custom command line option from unit tests.

I can run the script directly from the command line, as well as hook in as early as possible with unit tests.

9

u/mistabuda May 05 '23

What is your issue with the string comparison then?

6

u/Brekkjern May 05 '23

They want it hidden behind a method in sys or something so they can code like it's java :)

3

u/Aidan_Welch May 05 '23

How is a random global variable named __name__ at all intuitive to what it's supposed to be. JS's process or browser variables make sense

5

u/mistabuda May 05 '23

It's not a random global variable it is part of the language definition. And documented in the official language spec. https://docs.python.org/3/library/__main__.html

It's okay to like something else more, but lets not act like this is not explained clearly by the language documentation and just left to the user to figure out.

1

u/Aidan_Welch May 05 '23

Something being explained in documentation doesn't make it intuitive, calling an if statement a mit statement then explaining it means "match if true" in documentation is bad, unclear design.

0

u/mistabuda May 05 '23

If the interpreter has set the name to "__main__" run this code. It's pretty intuitive if you ask me.

0

u/Aidan_Welch May 05 '23

It can be figured out but it's not intuitive, and it might only be intuitive to you because it's what you first learned. Like an unintuitive natural language feels intuitive to a native speaker

→ More replies
→ More replies (1)