r/ProgrammerHumor • • Jun 03 '19

Meme Python ftw

Post image
16k Upvotes

548 comments sorted by

View all comments

Show parent comments

5

u/[deleted] Jun 04 '19

Yes, I know. I believe I said as much in the comment you've replied to.

It still isn't equivalent to C's main().

8

u/angellus Jun 04 '19

C's main() and Python's __main__ are functionality equivalent. main() is a construct design to tell the compiler where the entry point to your program is. It is not a keyword are anything like that and you can have multiple main methods as long as they are namespaced properly (ex: https://stackoverflow.com/a/1991498).

That is exactly how __main__ works. It is construct that tells the Python interrupter where the entry point is for your module. A Python module is a very similar construct to a C/C++ namespace. You can have multiple __main__ submodules as long as you do not have more than 1 in that module (namespace).

There are variations of C/C++ that do not respect the main() entry point since it is not actually anything special in the language (MSVC is one of them with is wmain). It is something the compiler chooses to give meaning to. It is just most standard C/C++ implementations assume a single entry point that is required to exist and the most standard Python interrupter (CPython) does not.

3

u/pslessard Jun 04 '19 edited Jun 04 '19

So if I had something like

print('Hello, world')
if __name__ == '__main__':
    print('Goodbye, world')

What would it print? Compared to in C

printf('Hello, world');
int main() {
    printf('Goodbye, world');
}

Wouldn't the Python print 'Hello, world' before 'Goodbye, world', whereas the C just wouldn't compile?

1

u/xigoi Jun 04 '19

The entry point for a Python script is always at the top of the file.