Is this considered good form when writing cross-platform code?

#! /usr/bin/env python3

And how about this?

if __name__ == "__main__":
    main()

I've reviewed some recent sample snippets and not all of them use the shebang and the conditional main call, so I wasn't sure whether that is still considered good practice.

Thanks for all replies.

Yes, it is excellent style.

using main() is a waste as it is the same thing as putting it under if __name__ etc. Instructors use it to teach students about functions, and always use the same name so as not to confuse them. For "real programmers" each function has a specific function (makes sense) and has a descriptive name that says something about what the function does. Obviously main() is neither of these. And it is frustrating for debugging a progam when you go to the bottom to see what tests, etc. there are, and then find a main(), which means you now have to search through the program for it instead.

I don't agree with that. I think it is very convenient to have a main function. As soon as the code in if __name__... has more than a few lines of code, it seems logical to encapsulate it in a function or a class. For example this code may have private variables which don't need to become global variables. As this code thickens, additional functions may become necessary. Also this is coherent with other programming languages. Every C programmer will feel at home with a main() function.

The shebang line is of course very useful when you write cross-platform code or in case of
if __name__ == "__main__":
when you write a module and a test for it.

To throw in function main() is a leftover from the C days. C is looking for it to start, Python could care less.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.