Python's equivalent to Main calling program and subprograms

2010-12-01 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread Tim Harig
On 2010-12-01, goldtech goldt...@worldpost.com wrote:
 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()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread Jean-Michel Pichavant

Tim Harig wrote:

On 2010-12-01, goldtech goldt...@worldpost.com wrote:
  

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()
  

def main: raises a syntax error.

def main(): does not (python 2.5)

JM

--
http://mail.python.org/mailman/listinfo/python-list


RE: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread m b



 
  if __name__ == __main__:
  main()

What does this mean?

/Mikael
  -- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread Chris Rebert
On Wed, Dec 1, 2010 at 9:08 AM, m b sn...@hotmail.se wrote:
 
  if __name__ == __main__:
  main()

 What does this mean?

See http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm
(which is the 3rd Google hit for __main__)

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread Jean-Michel Pichavant

m b wrote:



 
  if __name__ == __main__:
  main()

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread Benjamin Kaplan
On Wed, Dec 1, 2010 at 12:08 PM, m b sn...@hotmail.se wrote:


 
  if __name__ == __main__:
  main()

 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread Hanif Jameel

On 01/12/2010 17:08, m b wrote:



  
   if __name__ == __main__:
   main()

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.



--
http://mail.python.org/mailman/listinfo/python-list


Re: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread Steve Holden
On 12/1/2010 12:08 PM, m b wrote:
 
 
 
  if __name__ == __main__:
  main()
 
 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
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread Tim Harig
 On Wed, Dec 1, 2010 at 9:08 AM, m b sn...@hotmail.se wrote:
  if __name__ == __main__:
  main()

 What does this mean?

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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's equivalent to Main calling program and subprograms

2010-12-01 Thread Bill Allen
Thanks for the explanation of main.   Some tutorials mention it, some
don't.   I have written some not trial Python programs and have never had a
real need to use that convention, but at least I understand it now.

--Bill

On Wed, Dec 1, 2010 at 1:13 PM, Tim Harig user...@ilthio.net wrote:

  On Wed, Dec 1, 2010 at 9:08 AM, m b sn...@hotmail.se wrote:
   if __name__ == __main__:
   main()
 
  What does this mean?

 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.
  --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list