I am currently working with Python and have been confused over the fact that functions are listed in __main__
. I have been looking over multiple python scripts to try to find a common theme as to what functions warrant a place in __main__
, but to no avail. Here I have a sample of my own code. firstfunction
and
anotherfunction
are the only two functions in my code.
def main(argv):
firstinput=""
secondinput=""
if len(argv) < 3 or len(argv) > 3:
print """"Please set to:
metisfinal.main(metisfinal.py, firstinput, secondinput)""""
return
else:
firstinput = argv[1]
secondinput = argv[2]
firstfunction(firstinput, dictionary)
anotherfunction(list, secondinput)
if __name__ == "__main__":
main(sys.argv)
(I think) I know that the arguments and __main__
call are correct, but firstfunction
and anotherfunction
always return errors (because their arguments are not globally defined). I'm positive that this is arising from a faulty understanding of __main__
, because all the other examples I've looked at, basically set up __main__
in the same manner.
What constitutes listing a specific function in __main__
? I have stumbled over some Python code that has upwards of 30 functions within it, but the programmer only listed 2 of those functions in __main__
. Likewise, sometimes a code will have classes in the main argument, like this one (Project
is earlier defined as an object class):
def main(argv):
filename = ""
outputfilename = ""
p = Project(filename, outputfilename, subdomainNames)
p.generateICs()
if __name__ == "__main__":
main(sys.argv)
Conceptually, I can't understand why all the functions aren't listed... don't all of them need to be run or is __main__
simply initializing something?
Am I looking at atypical code? What key concepts of __main__
am I missing? And once I do find what functions to put into __main__
, is there a specific way to format them?
It's not clear what you mean by "listed in __main__
". __main__
is not an entity in the source file. Rather, it is the name of the module, if you execute it directly. When you do if __name__=="__main__"
, you are telling Python to execute the code in that block if and only if the code is being executed as the main module --- that is, if it is a program being run. The code in the if __name__=="__main__"
block will not be run if the module is imported from another module.
Note that you do not "list" functions in that if block. Instead, you put regular program code in that block that you want to be run. Often this code just calls one function. Often people call that function main()
. But there is no special relationship between __main__
and main
. You can call the function anything you like:
def snicklefritz():
# This function will be run when you run the program
print "You ran the program!"
if __name__ == "__main__":
snicklefritz()
Try running this program (say, by saving it as "snicklefritz.py" and then doing python snicklefritz.py
from the command line). You'll see "You ran the program!" printed. If instead you create a separate file that does import snicklefritz
, the message won't be printed.
Note that there's nothing about "listing functions". For example, look at this program:
print "This will always be printed!"
if __name__ == "__main__":
print "This will only be printed if you run the file as a program!"
Here the if __name__=="__main__"
block does not "list" any functions. It just contains actual code that is run when the file is run as a script. People usually don't do this, though, because it's more tidy to have the code in a separate function instead of just sitting there "exposed" outside of a function.
As for other functions, you can define whatever other functions you like in your module, to be used either within that module, or to be used by other modules that import your module. Typically most of the functions in a module won't be used inside the if __name__=="__main__"
block, because they won't be part of the "main" function. Instead, they'll be other functions intended for use by other code. For example:
def otherFunc(x):
# Return x squared
return x**2
def snicklefritz():
# This function will be run when you run the program
print "You ran the program!"
if __name__ == "__main__":
snicklefritz()
otherFunc
is not used at all in the module. That's fine. It may be that someone will want to import your module and use otherFunc
themselves. Not every function has to be used within the same module, let alone be called from the if __name__=="__main__"
block.