r/ProgrammerHumor May 27 '22

Meme Welcome to hassle free coding

Post image
2.2k Upvotes

305 comments sorted by

View all comments

357

u/Bomaruto May 27 '22

Bye main, hello __main__

13

u/[deleted] May 27 '22

[deleted]

20

u/22draynor May 27 '22

8

u/[deleted] May 27 '22

[deleted]

18

u/[deleted] May 27 '22

Unless I’m mistaken (I’m not a python dev) that page is saying that there’s a variable python generates called “name” that will be the name of the module (file) where the code is executing. However, if the module is the entry point for the program (ie the script you ran to start the code executing) then that name variable will contain the value “main” instead.

Edit: Disclaimer: reddit has taken the two underscores before and after to mean I want to bold “main” and “name”, and I’m not going to bother to figure out how to correct that.

12

u/[deleted] May 27 '22

[removed] — view removed comment

2

u/[deleted] May 27 '22

Nice, and I should have known

5

u/[deleted] May 27 '22

Unless I’m mistaken (I’m not a python dev) that page is saying that there’s a variable python generates called “__name__” that will be the name of the module (file) where the code is executing. However, if the module is the entry point for the program (ie the script you ran to start the code executing) then that __name__ variable will contain the value “__main__” instead.

Edit: Disclaimer: reddit has taken the two underscores before and after to mean I want to bold “main” and “name”, and I’m not going to bother to figure out how to correct that.

Edit 2: Fixed, thanks u/nukedkaltak

3

u/DeKokikoki May 28 '22

And to finish the explanation so that people don't have to open the link: if you want to import a function or a class or something from a module, python needs to run that module. If you haven't used the "if __name__ == '__main__': main()" construct, that means the main code of that imported module will also be run which is almost never what you want.

1

u/debug4u May 27 '22

you explained it better than the actual article 😂

1

u/sekoku May 28 '22

Edit: Disclaimer: reddit has taken the two underscores before and after to mean I want to bold “main” and “name”, and I’m not going to bother to figure out how to correct that.

You're using the "old" editor/markdown. You need to escape character it:

__ or so. It's dumb, but that's how Markdown works.

4

u/22draynor May 27 '22

There are also lots of videos on the subject. best way to learn is to implement it yourself on a small scale and then explore uses for your new found knowledge.

3

u/coloredgreyscale May 28 '22

have you tried turning it off and back on?

(sleeping?)

1

u/[deleted] May 28 '22

🤦‍♂️I’ll try that and report back

2

u/Amidus May 28 '22

Oh, no, it's okay, it says it's easy.

...

What did it say? Lol

1

u/cobance123 May 28 '22

I think those called magic values or sth, and are special python values that determine some things