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?