Hey guys, some of you might know me from other sub sections of Dani Web. I am about (just started) to learn Python. And not just learn but possibly make it my prefered language over all. So, I've read the thread from vegaseat for beginners and it has a link to a couple of examples provided by google. This is the source code of the first one

#!/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0

# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/

"""A tiny Python program to check that Python is working.
Try running this program from the command line like this:
  python hello.py
  python hello.py Alice
That should print:
  Hello World -or- Hello Alice
Try changing the 'Hello' to 'Howdy' and run again.
Once you have that working, you're ready for class -- you can edit
and run Python code; now you just need to learn Python!
"""

import sys

# Define a main() function that prints a little greeting.
def main():
  # Get the name from the command line, using 'World' as a fallback.
  if len(sys.argv) >= 2:
    name = sys.argv[1]
  else:
    name = 'World'
  print 'Hello', name

# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
  main()

Okay, so I have a couple of questions concerning the syntax, also I noticed that this code is in Python2 while Python3 is now the standard I believe.

Anyway so here are my questions

This line #!/usr/bin/python -tthas # at start, isn't that a comment? But I think that line is required to be at the start of every script. I would appreciate a little information on this

Then I am a bit confused on this

 # This is the standard boilerplate that calls the main() function.
    if __name__ == '__main__':
      main()

Is this required for all scripts? Kind of seems like a main function in languages such as C,C++ or Java. But what is the actual meaning of the if __name__ == '__main__': , especially the underscores?

How would that be if I had implemented more functions, say I have printName and PrintNames, do I need to call them such as

     # This is the standard boilerplate that calls the main() function.
        if __name__ == '__main__':
          main()
          printName()
          printNames()

Thank you in advance guys

Edited 1 Year Ago by Slavi

The line

#!/usr/bin/python -tt

is not a python thing. It is a unix mechanism named shebang. If the file is made executable in unix, the first line of the file is parsed and if it contains the shebang structure, the OS knows that it must execute the program on the shebang line. For example executing this file in unix will start the process

/usr/bin/python -tt thisscript.py

A python file such as myscript.py has two different uses:

  1. it can be executed as a script by a command python myscript.py
  2. it can be used as a module by another script with a statement import myscript

When the file is executed, a special variable __name__ is defined. In the first case, this variable has the value '__main__' and in the second case its value is the current module's name 'myscript' in our case.

Hence the intended meaning of

if __name__ == '__main__':
    do_stuff()

is

if we_are_executed_as_a_script_and_not_as_a_module():
    do_stuff()

It is not required for all modules, but it is often useful. A main() function is not required in python. You can write arbitrary code in this block, you could even have several such blocks.

Edited 1 Year Ago by Gribouillis

Use of if __name__ == '__main__':

# use it for testing function1()
# allows the program to run or be an importable module
# __main__ is the namespace of the current program
# if a module is imported the namespace changes to the name of the module

def function1():
    pass

# test the module
# this code block will be ignored if imported as a module
if __name__ == '__main__':
  function1()
This question has already been answered. Start a new discussion instead.