Hacker News new | past | comments | ask | show | jobs | submit login

Why is this being downvoted? Classes might not be what everybody needs but this pretending that hello world can be written in 1 line is just inviting people to write sloppy code, false advertising is what it is.

In my experience namespace pollution because things are written outside a function is a very common cause of bugs and maintenance problems in production python code. People coming from C/Java/similar think that "if" has scope but it doesn't, only functions do. If i see "main()-code" outside a function during a code review i immediately disregard that whole script.

Any program that actually does something, except just printing hello world, would more likely look like this:

    import sys
    def main(args):    
        function_local_variable = "yay"
        print("hello world")

    if __name__ == "__main__"
        pollute_global_namespace = "yes"
        main(sys.argv)
Short of the braces and the variables i added for clarity it actually has more lines than the java-version.



> Classes might not be what everybody needs but this pretending that hello world can be written in 1 line is just inviting people to write sloppy code.

IMV, sloppy unstructured code is a fine place for many (especially very young) programmers to start, and it simplifies the onramp significantly (and, actually, for simple scripts, relatively unstructured code in python is sufficient for lots of real-world uses, too.)

  if __name__ == "__main__" 
...is the k of thing you need only if you are deliberately designing something to be used as both a script and a library, which there are very good reasons to do for many real world python files, but it certainly is not universally necessary. And using classes for hello world is excessive Java-ism.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: