2

Most of my Python scripts (mostly written for web-scraping/data science apps) follow this kind of format:

# import whatever packages
import x, y, z

# do some web-scraping and data manipulation

# write some niche function I need

# make some plots and basically end the script

This is all done via an interactive editor/console (like Eclipse). I basically write the code above, and then copy-paste the code below for testing.

Is there a more "standard" way to go about this? I know C has functions defined above the main function, and I see packages in Python with if __name__ == "__main__" conventions; is this the "appropriate" way to go about development? I suppose the core question is whether you want to be able to use the functions you write in other projects, as well.

blacksite
  • 12,086
  • 10
  • 64
  • 109
  • 4
    please take a look at [this](http://stackoverflow.com/questions/419163/what-does-if-name-main-do) topic – algor Feb 11 '16 at 19:08
  • 2
    The "if main" pattern is useful, because it lets you import stuff from this file into other python scripts and modules. http://stackoverflow.com/questions/419163/what-does-if-name-main-do – Håken Lid Feb 11 '16 at 19:09
  • `if __name__ == '__main__'` has *nothing at all* to do with code organization. As such, this question isn't sticking to a single topic, and is arguably too broad. – Charles Duffy Feb 11 '16 at 19:13
  • @HåkenLid: Also, it helps when you want to use `pydoc`. – zondo Feb 11 '16 at 19:25

2 Answers2

3

The main reason to use if __name__ == "__main__" is so that code under it doesn't run if the file is imported from another file. When a file is imported in python, it simply run through the interpreter as if it had been pasted in at that point, but usually you just want the functions defined in the file, and not to run any code in the file.

For example, if you want to test the functions in your file, or use them in another file, you'd put the code that actually runs the functions under the if __name__ == "__main__".

Since you're writing code for your own consumption, it's totally up to you whether you want to put your code under if __name__ == "__main__", but it's recommended if you want to write your code in a reusable way.

There is no official, universally followed standard in Python with regards to how to organize your code. In relation to your specific question, for readability, and to follow common style patterns, I would recommend you put your functions at the top, and group your web scraping, data manipulation, and plots at the bottom.

dmi_
  • 1,187
  • 2
  • 12
  • 26
2

I've seen Python packages with if __name__ == "__main__" written in different places within the file before, and a main() defined elsewhere in the same file. I believe, however, that it should go at the bottom of the file you intend to have as your main.

For example, it could look something like this:

import everything

define class1
    define function1

define class2
    define function2
    define function3

define main()
    call function3
    call function4
    do_things

define function4
define function5

if __name__ == "__main__":
    run_main()

The sample above is a very generic version of something I've actually got running at work. When executed, it makes calls to a few other files, and works beautifully. It even has a check in the if __name__ == "__main__": block to check for a minimum version of Python.

Hope this helps.

DJGrandpaJ
  • 571
  • 3
  • 7
  • 20
  • The first sentence of your bottom paragraph is not true at all. You could also stand to add some reasons why it's useful to have the `__name__ == '__main__'` (there's some in the comments on the question). Anyway, though, this is a generally good file structure and decent advice. – Two-Bit Alchemist Feb 11 '16 at 19:23
  • 2
    *“If you don't include the if __name__ == "__main__": anywhere in your code, you won't be able to run it.”* that is incorrect – as long as you call run_main() it is fine. Without the if, however, you will not be able to *not* run your code ;-) – Jonas Schäfer Feb 11 '16 at 19:24
  • `If you don't include the if __name__ == "__main__": anywhere in your code, you won't be able to run it.` Eh, what? – zondo Feb 11 '16 at 19:26
  • I should've proofread my response! Sorry, guys. Thanks for pointing that out. (Edited to fix that!) – DJGrandpaJ Feb 11 '16 at 19:28