About #!/usr/bin/env python, at least for me, it is rare to make Python only one-file. Often, there are modules, probably with top-level app.py. So, I only put shebang once.
Check out the main() function there. It's defined at the top of the file and it calls lower level functions there.
Regarding the hash bang, yes I just do it out of habit. I often use python to replace bash to write small command line tools, which in my case, often in single files.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
But, does it even possible for some languages that have functions are first class citizen, such as Python?
I mean, class methods?, no problem. JavaScript with
function
keyword?, no problem. But what about the others?Python, yes. C/C++, yes (you declare the functions first in header). Bash, yes.
Python?
This does not work. (If you put
if __name__ == '__main__':
at the bottom, it will work, though.)But this does work in JavaScript. Don't know whether I should do it, though.
Well that's true.
if __name__
must be written on the bottom. However here's I always do it in python:
Ordering functions from the highest abstraction to lower works this way.
It is always nice to see a runnable code, and if possible, IRL open source projects. Thanks.
And I also want to say, this also works regardless of being outer scoped.
main()
has no real meaning. Butif __name__ == "__main__":
as well as anything in__main__.py
do. main() may help in unittesting, though.#!/usr/bin/env python
, at least for me, it is rare to make Python only one-file. Often, there are modules, probably with top-levelapp.py
. So, I only put shebang once.Yes,
main()
is just common to see. By no means it has to be calledmain()
.Here's an example from docker-compose:
The entry point is here
It doesn't even use the usual
if __name__ ...
which calls this module
Check out the
main()
function there. It's defined at the top of the file and it calls lower level functions there.Regarding the hash bang, yes I just do it out of habit. I often use python to replace bash to write small command line tools, which in my case, often in single files.