doubt regarding main function

Laszlo Zsolt Nagy gandalf at geochemsource.com
Thu Apr 7 07:41:17 EDT 2005


>if __name__ == '__main__':
>     test() 
>
>What is the advantage of this way calling a
>function.
>  
>
For example, you can have a python script that has this function:

def search(keywords,engine='www.google.com')

At the end of the script, you can have this:

if __name__ == '__main__':
     import sys	
     print search(sys.argv[1]) 


E.g. if you run the script as a program, then you can use it for 
searching and print the results.
But you can use it as a library, like

import searchengine # This won't execute the search method because 
(searchengine.__name__ == '__main__') evaluates to False

# Use the module here
results = searchengine.search('Python')

Of course this is just one possible use. For example, you can run a self 
testing function (when not used as a module).

Best,

   Laci 2.0


-- 
_________________________________________________________________
  Laszlo Nagy		      web: http://designasign.biz
  IT Consultant		      mail: gandalf at geochemsource.com

     		Python forever!





More information about the Python-list mailing list