In Python why a function called main doesn’t have any special significance like it has in C and Java?
What if a programmer switches from C or Java to Python. Should he keep
using main in Python also like in C or Java as it’s his style now to do
the programming or in a broad sense it is somehow harmful for doing
programming in Python?
Edit: I have gone through this article where it was mentioned by a very good example why first time programmers should refrain main in python main in python harmful.
You should also be asking, why in C and Java does
mainhave a special significance. It’s just a choice on the part of the language designer.maincould well have been calledstartorbeginbut somebody chosemainand it stuck.In Python there is no reason why you can’t call a function
mainand have it be the start point of your program. However, Python has its own syntax to identify whether a certain file is the equivalent ofmain:This is typically wrapped as part of an
ifand could simply have a single line within calling yourmainfunction that actually starts your program.Part of the design of Python and many (all?) scripting languages is that code can simply be written inline. You don’t have to wrap everything in a function. As such, many simple scripts do not require any functions at all. A cron job for example that rotates log files could just be written as a block of code in a python file with no functions being defined.
In that scenario, the
mainmethod just isn’t required.Not requiring a
mainin many ways makes the language more flexible, especially for simpler tasks.ADDENDUM:
To add some context to your edit. That article presents a very poor argument. In reality function name collisions are not uncommon as there are many modules that do the same or similar things (not so much in core but as soon as you start using pip you’ll encounter the odd collision). Therefore it is beneficial to use descriptive function names and avoid ever doing a
from foo import *.In the same way that C++ programmers generally consider it bad form to pollute your namespace with
using namespace std, Python programmers typically consider it bad form to pollute your namespace withimport *, especially as it can cause a snowball effect if used everywhere.Finally, you’re unlikely to call 2 functions in your program
main. You’re much more likely to have name collisions elsewhere. The real danger is the wildcard import, not themainfunction.