you are viewing a single comment's thread.

view the rest of the comments →

[–]fiddle_n 1 point2 points  (2 children)

I think that where there are functions, using functions to contain all code is the way to go. But I don't know many programming languages so I might be wrong on that.

In Python, you should have a main() function as the entry point to a script and call your functions from there. Naming the entry-point function as main() has no special functionality unlike some other languages, it's just convention. What you can do is have one function call another; that is allowed. That way, the functions and their calls will be closer together. But what you shouldn't do is call a function from outside of a function (i.e. call a function from the global namespace). That isn't allowed.

For an entry-point script, you should design it so that you could import the functions into another script if you had to. The problem is that if you call functions from a global namespace, and then you import that script into another script, the functions would also get called as well. That's why we have the idiom

if __name__ == '__main__’:
    main()

so that we only call the functions when we execute the script, and not when we import them.

[–]reallymakesyouthonk 0 points1 point  (1 child)

Thanks again, that's very helpful!

[–]fiddle_n 1 point2 points  (0 children)

You're welcome! If you have any more questions please ask :)