Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Double Unders

Understanding Double Unders __main__

Can someone with experience give me a hint on this? I just want to know whether or not it is considered good practice or standard to add a main() function and dunder check to your source code files when designing a python package (multiple files that work together):

Edit: And what is the standard code order here?

  1. import statements
  2. class and function definitions
  3. main function
  4. dunder check
def some_module_function():
  pass

def main():
  # main code goes here
  some_module_function()

if __name__== "__main__":
  main()

Or can I just have my 'main code' bare in the file?

2 Answers

I personally like creating a function called main and calling it from the if __name__ == '__main__' bit. Some people have other preferences. It isn't 100% standardized, but most people prefer having a main function.

And yes, usually the structure of a program goes as follows:

  • Imports
  • Classes
  • Functions
  • Main function
  • if __name__ == '__main__'

@Alexander Davison Thank you for the quick reply. Really appreciate it.

No problem :+1: