> plenty of people write Python scripts with no main() function and no
> need to test for `if __name__ == __main__`.
>
> And there is nothing wrong with that.


Indeed, I think there is something “right” with that.

The if __name__ block is only required for a Python file to be both a
module and a script.

That’s actually a pretty uncommon thing— if it’s a module to be imported by
other modules, then it probably should be part of a package, and if the
functionality needs to be made available as a script, there can be another
module just for that.

If a script is complicated enough that it should be built as multiple
functions, it can call the main function after define ing what’s needed,
but no need for the __main__ guard.

Someone in this thread (the OP?) wrote that most of the modules they right
are both importable and scripts — I’d like to hear more about those use
cases, because that diste seems like a bad practice to me.

-CHB



> In C terms, this code is not even possible:
>
>     print("Hello")
>
>     def main():
>         print("Goodbye")
>
>     __main__(main)
>     print("Why are you still here? Go home!")
>
>
> since you cannot have code outside of a function that is called before
> the entry point main() (declarations and macros excempted) or after it
> returns.
>
> I don't think it is a good idea to give programmers coming from other
> languages the false impression that Python behaves the same as the
> language they are familiar with, when it doesn't. The entrypoint to
> Python scripts is not main(), it is the top of the module.
>
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/NRSVRVWBX3WXQLYWCDSTOB5FJPIPX7Z3/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-- 
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/GTYZU2HIHMI3XBZJ5CZGOLUKT3VFWNLU/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to