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: http://stackoverflow.com/questions/419163/what-does-if-name-main-do

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,

python myscript.py

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 myscript.py 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 one.py
def func():
    print("func() in one.py")

print("top-level in one.py")

if __name__ == "__main__":
    print("one.py is being run directly")
else:
    print("one.py is being imported into another module")
Although it's quite hard for the FDA to enforce this law in another country, it is still  viagra sildenafil 100mg considered to be wrong. You need time and resources to blog  sildenafil tablets effectively. Characteristically, parental reactions http://djpaulkom.tv/dj-paul-hiphopdx-interviews/ india viagra for sale at the first staff recognition meeting fall into three categories. Because of these lumps the blood does not flows properly and because the blood is not reached to those parts sufficiently then erection doesn t occurs and if some how you manage it check my djpaulkom.tv now online levitra does not let the patients feel awkward and offer an efficient way to consume the drug for ED treatment. 
# file two.py
import one

print("top-level in two.py")
one.func()

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

Now, if you invoke the interpreter as

python one.py

The output will be

top-level in one.py
one.py is being run directly

If you run two.py instead:

python two.py

You get

top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly

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

Credits: Adam Rosenfeld

Review on Startup.com

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

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

Startup.com Lessons Learned

We watched the documentary Startup.com 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?
The bladder is composed of many http://www.icks.org/data/ijks/1482965794_add_file_3.pdf viagra order online transitional cells and their abnormal and at times leads to ultimately the breakdown. Diabetes is indeed one of the most debilitating conditions that may affect not only your general health but also your social, emotional, and psychological wellbeing. prescription viagra prices You simply need to know how to jump generico levitra on line rope. They are all the time available to hear the queries of people related to Ayurveda, most of them consists the questions having this element- WHY? Being the team of the doctors the cheapest cialis http://icks.org/n/data/ijks/1482459178_add_file_6.pdf working on the surgery for both donor and patient.
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.