Python series: What does the if __name__ == “__main__”: do?

In python, unlike in C++/C that always has void main() to run the program,
it is usually stated as: if __name__ == “__main__” :

This post is just a note from stack overflow, so basically I just repost the same question in this link:

When the Python interpreter reads a source file, it executes all of the code found in it. Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value “__main__”. If this file is being imported from another module, __name__ will be set to the module’s name.

When your script is run by passing it as a command to the Python interpreter,


all of the code that is at indentation level 0 gets executed. Functions and classes that are defined are, well, defined, but none of their code gets ran. Unlike other languages, there’s no main() function that gets run automatically – the main() function is implicitly all the code at the top level.

In this case, the top-level code is an if block. __name__ is a built-in variable which evaluate to the name of the current module. However, if a module is being run directly (as in above), then __name__ instead is set to the string "__main__". Thus, you can test whether your script is being run directly or being imported by something else by testing

if __name__ == "__main__":

If that code is being imported into another module, the various function and class definitions will be imported, but the main() code won’t get run.

As a basic example, consider the following two scripts:

# file
def func():
    print("func() in")

print("top-level in")

if __name__ == "__main__":
    print(" is being run directly")
    print(" is being imported into another module")

# file
import one

print("top-level in")

if __name__ == "__main__":
    print(" is being run directly")
    print(" is being imported into another module")

Now, if you invoke the interpreter as


The output will be

top-level in is being run directly

If you run instead:


You get

top-level in is being imported into another module
top-level in
func() in is being run directly

Thus, when module one gets loaded, its __name__ equals "one" instead of __main__.

Credits: Adam Rosenfeld

Review on

hello, this time I would like to post one of my review about movie

this review is part of the assignment of my class in CMU, which is 45-805 Entrepreneurship for High Growth Companies Lessons Learned

We watched the documentary in class. To help me get to know you better, I would like you to reflect on the lessons you learned from this documentary. Tell me what you thought about the people, the company, and/or what was happening during this unique time “the internet bubble”. Reflect on what you would have done in the same situation or how you experienced something similar in your own life.

About the Company

Kaleil made a decision to leave his job in Goldman Sachs to start his own internet company, originally named Public Data Systems, together with his childhood friend, Tom Herman. At the beginning, May 1998, there were only 8 employees.

The idea of the business was to provide people an easy access so that they can do transaction with state government institutions such as parking tickets through internet portal. The market was huge and the potential to provide high quality of service to a notoriously inefficient sector was breathtaking.

This movie talks about the “dot com” phenomenon in the beginning of 2000s where the technologies applied in innovative ways sparked enormous enthusiasm amongst investors who were afraid to miss the next big thing.

Actually, the idea and the moment were perfectly matched. Making people’s life easier by providing online facilities to do governmental things so that they would save time and money, and at that time the booming of the internet was exciting for many people especially because of the technology behind it. But what went wrong?

In my opinion, first, the company grew too fast, from only 8 to over 250 employees in about only 16 months. Second, their technology was not ready. They underestimated the time needed to build the core system, which was the technology of their web site. They should have spent more time for the development and the testing of the web site before going live, no wonder their web site was worse than their competitors at that time. The readiness of the company was seen the most in the part in which the company was broken into and stuff was stolen from the company. For one thing when running a company that deals with personal information in car tickets and sensitive material such as credit card numbers and addresses security is a huge priority. Then when coming to realize what was stolen and trying to observe if any information was compromised the company during filming was only able to find out about, about being the key word what time the break in happened. Third, although they have succeeded raising money for about $60 million, it means they also brought a loss of control as outsiders gained control of the company’s board of directors.

About the founders: Kaleil and Tom Herman

I think that they were too optimistic in their expectations of the income from clients. Kaleil was the obvious leader and throughout the film showed his leadership style in a very obvious way. In the film there are meetings and retreats that the company holds to bring them together. In all the meetings it is him and his enthusiasm that brings people in the company to share in his passion. Kaleil is truly dedicated to his project and like a good captain goes down with his ship. As the company falls down into debt and eventually dies, Kaleil attempts to stay positive and upbeat but it is too late.

From the relationship point of view, Kaleil was really poor at it. He lost his best friend and his girlfriend. The girlfriend however we could see from the beginning that that was probably not going to the last but the Tom and Kaleil relationship was almost harsh to watch. Kaleil’s passion towards the project was amazing but in any venture is necessary to also have a life outside of the company. If one does nothing other than work eventually you will go crazy no matter how much you enjoy what you are doing. Kaleil did not so this and it was not until the company went completely under that we realized what had happened.

Tom Herman should not have been the CTO (Chief Technology Officer), enough said. They should have hired someone with a great deal more expertise in the technology.
At the beginning, sometimes Tom was also did not share his vision about the company with Kaleil. When Kaleil talks to the potential investors about building a web portal for parking tickets, Tom interrupted with talking about installing digital cameras inside a building.

What you would have done in the same situation

By learning from the Govworks experience, if I would like to start a business, first, I do not want to build my company with my best friends since it will risk our friendship. Second, I would be more focused on the product that I would like to offer than raising the money from the investors. What I have seen in the movie, they often talk about money, money and money, when they should have been talking more about the technology. I will try to fund the company mostly from myself so that I have the full control of my company. At the end, I will hire some real expertise in technology so that he is really dedicated to what he does, unlike what had happened with govWorks.