Learn python : Understand the main

Being a Software engineer, i came across many coding language.Most of the time
we want to learn a language, and start looking for tutorials or books. In some sense,
inital stage as a new learner we pick up, learn about data types,funciton modules etc.
Since for a new developer, its quite difficult to reach till end of the book, or without
having any project or motivation, most of us ended up in between.
I thought of going with a random approach, where i will see some pre-written code.

We will see the code and try to understand the concept.

In this start, I have seen one of the python program which was available in esxi.This python code was available in /bin/vmfsfilelockinfo.
After seeing this code multiple time, i found that code starts from main function just like C.

if __name__ == ‘__main__’:

”’defined main function,program starts from here”’

one of the blog it was explained very well.
http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm

This gives us the starting point in python, as well code can be executed standalone or can be reused in another code.

C:\Users\amit>cat test.py
def add(x):
return x + x

if __name__ == ‘__main__’:
print “test: add(42) ==”, add(42)

C:\Users\amit>python test.py
test: add(42) == 84
In above example this is working as standalone program. We can make this code as reusable code as well
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import test
>>> test.add(4)
8
>>>

In above part of code, we have imported test.py as module “import test”

In test.py i have defined one function add, which i called as test.add(value), which return me the value.