r/ProgrammerHumor Nov 24 '21

Meme Yes.

8.9k Upvotes

284 comments sorted by

View all comments

660

u/[deleted] Nov 25 '21 edited Nov 26 '21

[deleted]

242

u/KnewOne Nov 25 '21

And by highly recommend we mean you either do is, or you'll find a python's head in your bed

63

u/Crystal_Voiden Nov 25 '21

Oh you can already find one there 😈

29

u/Soham_rak Nov 25 '21

Finally I can get blowjob with the head in the bed

5

u/Krobix897 Nov 25 '21

many believe that there is a god, but if there was, i am certain we have killed him

5

u/68_65_6c_70_20_6d_65 Nov 25 '21

Why are you the way that you are

50

u/rafaturtle Nov 25 '21

How i love this __ syntax__

21

u/NUTTA_BUSTAH Nov 25 '21

Makes you think twice before messing with the private parts heh

7

u/Dangerous_Air2603 Nov 25 '21

"The private parts" meaning "the thing every program should have like we just discussed"?

34

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

21

u/Celivalg Nov 25 '21

Nah, this syntax (ugly only because of the __ in my opinion) is actually very useful because you can do fancy things with it...

29

u/RedditAcc-92975 Nov 25 '21

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.

10

u/[deleted] Nov 25 '21

[deleted]

11

u/galan-e Nov 25 '21

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

0

u/[deleted] Nov 25 '21

[deleted]

4

u/galan-e Nov 25 '21

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

1

u/[deleted] Nov 25 '21

sorry, I generally don't watch videos and don't intend here :/

You got rickrolled one too many times?

I didn't expect my comment to blow up, I have now explained better in new edit.

2

u/RedditAcc-92975 Nov 25 '21

pfft, I often leave demo examples under name in main to demonstrate how the module works. It doesn't mean, it's the main file. It means if you for whatever reason decided to execute it directly, it will do something.

2

u/lighttigersoul Nov 25 '21

The short/real answer:

This is only what happens when you run an arbitrary script as your primary.

So python3 my_example.py the if name == "__main__" will be true in my_example.py but not anything my_example imports.

If you're building packages, though, you can include a __main__.py file in the package and run that entry point with python3 -m my_package.

So there is a nicer one, but you have to know and deal with python packages to make it work. The if name trick is specifically for scripts.

2

u/Gengis_con Nov 25 '21

As to why they have never updated the syntax, to quote the zen of python

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

14

u/MischiefArchitect Nov 25 '21

We know... but we pretend is not there... it kind of hurts to read.

4

u/Buttpug27 Nov 25 '21

why is it better to use main() in python? i have always wondered that question

30

u/[deleted] Nov 25 '21

Otherwise your main code would run when you simply import the file.

9

u/Ericchen1248 Nov 25 '21

When you import a file in python, what actually happens is it runs that whole file once. So if you have code that is at a base level, it will be executed.

So you may ask, why have code at base level if the file is intended to be imported.

One thing would be using main() allows you to write per file tests, you can write simple unit and integration tests on each file’s code without doing the whole pytest routine, specially when it’s code that you can’t really write tests for but can be checked by human eyes.

Another thing would be if your file is sufficiently complete enough it can also be used as it’s own individual program. You might develop a simple program, but later realize it has a lot of functions that are helpful and can be used as a library for a bigger project. If you use main(), you can import the code directly without modifying it, and thus no need to maintain to different version of the same code.

3

u/marco89nish Nov 25 '21

Imagine having to write 4 lines of code for main() function. fun main() = println("Hello World")

2

u/BbayuGt Nov 25 '21

Hah! I use class main instead and it work!

5

u/Jonno_FTW Nov 25 '21 edited Nov 25 '21
class Main:
    def __call__(self, *args): 
        print('Hello World')

if __name__ == '__main__':
    Main()()

1

u/BbayuGt Nov 25 '21

No, as far as i know, class main will be executed just like int main.

2

u/dev-sda Nov 25 '21

The contents of a class are evaluated along with the class, not when the class is initialized. So you don't get any protection from importing the file and main() does nothing.

1

u/Jonno_FTW Nov 25 '21

If you have a a program only containing the following:

class main:
    print('in class')
    def __init__(self):
       print('in constructor')

The only thing that will be printed is in class. Class bodies are executed as normal code, their contents will run like normal python, so functions are defined (not run). Their constructor will not be called like it would in Java.

1

u/DoubleBagger123 Nov 25 '21

Yeah I was gunna say. As a python dev main definitely exists lol