Python's equivalent to Main calling program and subprograms


G

goldtech

Hi,

Could someone link me to info - I'm sure this is commonly done:

"Long ago" with Fortran and Pascal there was a pattern used a lot. It
was like:

Start
Main
Global Var
Subprogram1
Subprogram2
Subprogram3
End of Main
End

The global var was a var that all the subprograms could access. I
wanted a container for several scripts that run in succession one
after dhere other. This has to be newbie stuff - sorry to ask. If you
could give me some keywords or link me or provide a simple example I
would be grateful.

Thanks
 
Ad

Advertisements

T

Tim Harig

Start
Main
Global Var
Subprogram1
Subprogram2
Subprogram3
End of Main
End

module_wide_var = value

def Subprogram1:
# code

def Subprogram2:
# code

def Subprogram3:
# code

def main:
Subprogram1()
Subprogram2()
Subprogram3()

if __name__ == "__main__":
main()
 
J

Jean-Michel Pichavant

Tim said:
module_wide_var = value

def Subprogram1:
# code

def Subprogram2:
# code

def Subprogram3:
# code

def main:
Subprogram1()
Subprogram2()
Subprogram3()

if __name__ == "__main__":
main()
"def main:" raises a syntax error.

"def main():" does not (python 2.5)

JM
 
J

Jean-Michel Pichavant

m said:
What does this mean?

/Mikael
__name__ is an attribute of the module. Usually it is set to the module
name, except when the module is acutally executed as the entry point, in
that case __name__ is set to '__main__'.

foo.py:
print __name__

> python foo.py '__main__'

> python -c "import foo"
'foo'

This is a very basic concept, you should read the python tutorial.

JM
 
B

Benjamin Kaplan

What does this mean?

/Mikael

Every module has an attribute called __name__. Normally, it's the name
of the module itself. However, the module being run as a script
(rather than imported) is called __main__. You can use if __name__ ==
"__main__" to have certain things only run if the file is executed
directly.

---- a.py ----
print "I'm A"
if __name__ == '__main__' :
print "I'm the main script"

--- b.py ---
import a

$ python a.py
I'm A
I'm the main script
$ python b.py
I'm A
 
Ad

Advertisements

H

Hanif Jameel

What does this mean?

/Mikael
Python will not cause the main() function to run automatically when you
execute the script, it has to be called.

__name__ is a special variable which is set by the python interpreter
when it runs a source file. If the source file is run directly(as
opposed to being imported), then the value of __name__ will be set to
__main__.

so

if __name__ == "__main__":
main()

checks to see if the source file be being directly executed, and if it
is, calls the main() function.
 
S

Steve Holden

What does this mean?

/Mikael
It's a standard way of allowing programs to test themselves. When a
module is imported its __name__ attribute us bound to the name of the
module. When the module is run as a main program (from the command line)
its __name__ attribute is set to "__main__".

The main() call just calls a function that (presumably) tests the
functions the module provides.

regards
Steve
 
Ad

Advertisements

T

Tim Harig

It is a Python idiom and a good practice. Strictly speaking it is
unnecessary. Python doesn't recognize any functional initialization
vector other then the start of the file. When Python loads a module,
it executes anything it finds in the module scope (Anything not in the
body of a class or function declaration). Using a main function is just
a convention. You could just place all of your main level code in the
module scope:

def Subprogram1():
# code
def Subprogram2():
# code
def Subprogram3():
# code

# main code

or equivilantly, always execute main():

def Subprogram1():
# code
def Subprogram2():
# code
def Subprogram3():
# code
def main():
# main code
main()

Both are valid from Python's point of view.

The 'if __name__ == "__main__":' idiom is used, because it allows the
module to be loaded without running main(). This is useful if you wanted
to use Subprogram2() from another program. Even if you don't forsee using
any of the subprograms (functions to Python), this can be useful when
writing test code as you can import the program as a module to test its
classes or functions separately.
 

Top