开发者

Python's Equivalent of "public static void main"

开发者 https://www.devze.com 2023-01-01 13:18 出处:网络
What is Python\'s equivalent of \"public static void main(String[] args) { ... }\"? I remem开发者_StackOverflow中文版ber having used it in the past and then forgot.

What is Python's equivalent of "public static void main(String[] args) { ... }"? I remem开发者_StackOverflow中文版ber having used it in the past and then forgot.

What I'm basically trying to remember is a function which I think included some underscores (__)...

thx


#!/usr/bin/env python

import sys

def main(args):
    print args

if __name__ == '__main__':
    main(sys.argv)

edit: emulate a void return.


This is a commonly-used idiom, but it is NOT equivalent to Java's public static void main(String args[]). All Python modules execute from top to bottom all statements at the module's scope. For most imported modules, these statements are usually limited to class and method definitions, which set up values in the module's namespace, but don't actually execute the functions' code. But if you put a statement at module scope like SPECIAL_CONSTANT = 42, then this will be run immediately when the module is imported or run standalone - no need for main() or __main__ or anything. Try putting a print statement at the top of a simple module. Whether you import that module or run it by itself from the command line, the print statement will always execute.

What is nice about this idiom is that it allows you to embed some simple tests or demo code right in with a library that is typically imported by some module written by your customer. Maybe something like this:

# special_super_duper_module.py

# this next statements will run, even though not in main() or set off
# with "__name__ ==" tests or any such thing
print("You are using special_super_duper_module.py, written by Felix the Cat")
SPECIAL_CONSTANT = 42

def super_duper_function1():
    pass

def super_duper_function2():
    pass

if __name__ == "__main__":
    print ("You are running special_super_duper_module.py interactively")
    assert super_duper_function1() == None, "expected None, got non-None result"
    assert super_duper_function2() == None, "expected None, got non-None result"
    # doctests or unittests could also go here, or just a simple demo

When the module is imported by some code that wants to use your super duper functions, then the little banner at the top will print, but the code that is conditionalized by if __name__ == "__main__" won't get run. But if your user runs the module directly using the python command, then the embedded test or demo code will get run.

In contrast, public static void main(String args[]) is a special method signature that tells the Java VM what what method of what class to start with. Python just starts at the top of the named module and begins running from there. That's why you only need to write:

print("Hello, World!")

in a module all by itself to write your first Python program.


if __name__ == "__main__":
    ....do your thing


Oh!

if __name__ == '__main__':


The classic versions above are most commonly used, but for something more comprehensive, check out Guido van Rossum's blog post:

All Things Pythonic: Python main() functions (also view the comments for more)


It could be something like this...

import sys


class Main:
    @staticmethod
    def __init__(*args: str) -> None:
        print("Hello world")
        return


if __name__ == '__main__':
    Main()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号