Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

you don't need it until you get to importing modules.

hello world in python is literally just print("hello world!")



You don't even need it afterward, unless you want a file to be both an importable module and an executable script.

Arguably you could be a happy Python programmer without this capability.


I never understood why people bother with this when writing their python main file, which is never going to be imported because it doesn't make sense in the first place.


REPL-based development. You might be developing a script, but notice a bug. If you import your script as a module, you can run the function you’re debugging, figure out what’s wrong, edit the file, and then reimport the module (using importlib.reload).


Okay, that's quite convincing, thanks!


Because I dislike interspersing executable statements at module level with function definitions. I’d rather have all code live inside functions and have one main entry point for the script. It’s just a personal preference, nothing wrong with not doing that though.


But you could have a single main() call without the if statement, couldn't you?


Isn't that hugely elaborate compared to languages where top level statements are a function, functions return their last statement, and the return value of the entry function is sent to stdout unless invoked in special ways? That print() around the "hello world!" does not appear any less superfluous than all the public static void you can throw at a javac 1.0


You mean a language like... Python?

  >>> 'hello world'
  'hello world'


Exactly like that (well, almost exactly: if python includes the quotes then it's not really equivalent to print()).

The point was that by including that print(), people already do say yes to including "like you'd do in a real program" ceremony in their supposedly minimal examples.




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

Search: