December 23, 2020 at 12:00 am
Comments posted to this topic are about the item Starting a Python Program
December 23, 2020 at 3:06 pm
This was removed by the editor as SPAM
December 29, 2020 at 9:55 pm
Not trying to be the semantics police so I'll relinquish the stolen badge right after this, but since I struggled thinking this was a trick question I thought I'd share...
Technically, it's the first statement, not a function at all... A function will not be executed by itself just because it was defined, even if you name it "__main__" or "main" Python won't execute the function without it first being called. You can name a module __main__.py in a package and that file will be executed with the -m argument, but still not a function.
The "if __name__ == '__main__':" is a command/statement not a function (not sure what the right term here is).
The print statement below would not run just by existing in a function named __main__
def __main__():
print('This is my main function')
For that to work you need a statement or command that calls __main__()
def __main__():
print('This is my main function')
__main__()
or
def __main__():
print('This is my main function')
# ensures this only runs when executed as a script/program and not when being imported as a module
if __name__ == '__main__':
__main__()
-
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy