In python, if you want to write a script for sequentially executing a series of small tasks, then there is absolutely no to write a function to store them. Just put each on a line yourself; or use an expression delimiter, for example ; (not recommended, but you can do it like this), as well as:
task1 task2 task3 task4
or
task1; task2; task3; (again **not** really recommended, and certainly not pythonic)
In your case, your code may be facing something like:
print('boo') print('boo2') print('boo3')
and it will still act as you expect, without the main() method, since they will be evaluated sequentially.
Please note that the reason you can create a function for these series of tasks is:
- to present a nice interface (for code clients),
- or encapsulate repeating logic
- There may be more uses, but the first thing I can come up with and serve to prove my point.
Now, if you are forced to write code similar to the main() method in other programming languages, please use the following python idiom (as indicated by other users so far):
if __name__ == '__main__': doSomething()
The above works as follows:
- When you
import a python module, it gets the string (usually the name under which it was imported) assigned as its __name__ attribute. - When the script is executed directly (by calling python vm and passing the script name as an argument), the
__name__ attribute has the value __main__ - Therefore, when you use the above idiom, you can use the script as an
import plugin as you wish, or simply execute it directly to have a series of expressions under if __name__ == '__main__': evaluated directly.
If you feel the need to dig out additional information, my sources were as follows:
source share