Python for C++ or Java Programmer
Question: I have a background in C++ and Java and Objective C programming, but i am
finding it hard to learn python, basically where its "Main Function" or from
where the program start executing. So is there any tutorial/book which can
teach python to people who have background in C++ or Java. Basically something
which can show if how you were doing this in C++ and how this is done in
Python.

OK i think i did not put the question heading or question right, basically i
was confused about the "Main" Function, otherwise other things are quite
obvious from python official documentation except this concept.

Thanks to all


Answer: When you run a script through the Python interpreter (or import that script
from another script), **it actually executes all the code from beginning to
end** \-- in _that_ sense, **there is no "entry point" to a Python script**.

So to work around this, Python automatically creates a `__name__` variable and
fills it with the value `"__main__"` when you are running a script by itself
(as opposed to something else importing that script). That's why you'll see
many scripts like:

    
    
    def foo():
        print "Hello!"
    
    if __name__ == "__main__":
       foo()
    

where all the function/class definitions are at the top, and there is a
similar if-statement as the last thing in the script. You are guaranteed that
Python will start executing the script from top-to-bottom, so it will read all
of your definitions there. If you wanted, you could intermingle actual
functional code inside all the function definitions.

If this script was named `bar.py`, you could do `python bar.py` at the command
line and you would see the script print out `"Hello!"`.

On the other hand, if you did `import bar` from another Python script, nothing
would print out until you did `bar.foo()`, because `__name__` was no longer
`"__main__"` and the if-statement failed, thus `foo` was never executed.

