Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Best Practices for Python Main Functions

Now that you know the differences between how Python handles the various execution modes, it’s useful for you to know which best practices you can implement. You’ll learn about four best practices you can use to make sure that your code can serve a dual purpose:

  1. Put most code into a function or class.
  2. Use __name__ to control execution of your code.
  3. Create a function called main() to contain the code you want to run.
  4. Call other functions from main().

Gregory Klassen on March 10, 2020

Is it tmux or some other terminal setup you use in your presentation?

Dan Bader RP Team on March 10, 2020

@Gregory: I believe this is the built-in terminal of the VS Code editor. We have a dedicated course on it here :)

mattc on March 11, 2020

What happens if you import more than one module with a main() function? Does the namespace of the module keep the interpreter from getting confused?

SkyFox on March 11, 2020

Very often on the top of a file one defines module-level variables. Does it mean they become available from the scope of the importing script? Can they override each other?

pallavlearn on March 18, 2020

Do you record the audio and video separately or is it in one go?

Become a Member to join the conversation.