Check out a free preview of the full Practical Guide to Python course

The "Main Method" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina recommends adding a main method to Python programs to make them more modular. The main method will only execute when the program is running as a stand-alone application. If it has been imported in another module, the main method will not run.

Preview
Close

Transcript from the "Main Method" Lesson

[00:00:00]
>> Now that you have this foundational understanding of how Python works, we're gonna learn how to structure our code into Modules and work with different Libraries. We talked a little bit about the main method early on in the class. Once you start writing your more comprehensive Python programs, if you're not using a library that runs your code for you want to have a main method.

[00:00:25]
And the purpose of that main method is that it will only run when your code is executed as a standalone program. And because of how Python's import system works. If somebody else were to import your Python code, it is executed on import. So let's say we had a Python file like this called name lib, go ahead and copy this and let's create a new file and save this as main underscore lib dot UI.

[00:01:04]
If we run this code, it looks exactly like we expect it to the length is four and the uppercase version is Nina. But we want to write reusable code, because we went through all this trouble of writing this very, very useful library that has some methods in it like.

[00:01:23]
Getting the name for a length and the uppercase of that name. So how do we allow other programmers to use this? Let's say somebody else wrote a program called other program, and let's copy this, And save it as other, Program dot P1. We're going to import. As sorry here we call this main lib.

[00:01:58]
So let me just update this example. If somebody wants to open a pull request. Now's your time to shine. So let's go ahead and run this and we'll see I don't have Nina anywhere in here but this is printing out the uppercase version is Nina. What is going on?

[00:02:28]
I'm trying to use these methods from this main lib Module. Well, what's happening is when it is imported, all of the code in here is run including this stuff down here. And that was just stuff that I needed for myself. If somebody is using this module as a library, they probably don't care about it.

[00:02:54]
So that's why you want to wrap this code in a main method. So in order to do that you ask If the value of Dander name, which is one of those magic methods, magic variables is equal to underscore the string underscore underscore mean, then run this code. Now, If I run main lib.

[00:03:29]
I see the length is four and the uppercase version is Nina. If I go back to my other program and I run it again, it's not printing out anything about Nina anymore. That's because when I run this from the command line, I am hitting this code in the main method.

[00:03:48]
But when I import this programme into another file, that code in the main method is not being run. So that's an important distinction, that will help you write reusable code.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now