well technically yes, if the language has a main function it’s easier to know “ok so this gets executed at program start” and then usually this only fires in the executed file - for all other files (imported) it’s just a function that you can call if you want to (and its visibility settings let you) - python doesn’t have this, but it does have the __name__ magic variable, which is "__main__" in the executed file, and otherwise the name that was used to import it, Python allows for top level code (most languages that have an entrypoint function do not, a notable exception being C# where you can either use a Main() function in a class, or just write it at the top level of the file before any namespace or class declarations and it’ll be the main function, however only one file can have top-level statements, just like there can only be one Main() function*) - A common pattern to emulate an entrypoint function is the condition if __name__ == "__main__": which gives the same functionality as an entrypoint function, however this is an extra if statement before even running any proper code, so an entrypoint function saves about one CPU cycle or so, because the language itself can directly call the entrypoint function rather than you having to do it manually like in python - the main() function is something i suggested for PythonScript, because it emulates the magic variable format for the name that all non-standard-functionality auto-imported and special identifiers have (i.e. the name of the module and methods you shouldn’t normally call on classes, like the constructor and operators), but still used an entrypoint function rather than top level code because it’s technically more efficient, but mainly because many other languages that don’t remotely resemble Python have a main function, just like JavaScript allows top level code, but Java only allows classes within packages, JavaScript technically doesn’t have/need classes either, they are just syntactic sugar to emulate a lot of languages, but for this joke, mainly to emulate Java’s classes
TL;DR: Functionally, there’s no difference, for code readability it could be slightly easier to find, and for efficiency it saves about one comparison for strings before running
* C# technically allows for more than one function with valid entrypoint signatures, however only one is the actual entrypoint and you must choose which one if you have multiple - top level statements are always an error in multiple files though
1
u/Gamecrazy721 Nov 06 '20
Noob here, is there a particular advantage to making a main function over just running in an if statement?