What about PEP 299?
Hi, since I found myself writing "if __name__ == '__main__'" often these days, I wondered whether PEP 299 could be pronounced upon. I'm not proposing putting it into 2.5, but it should be relatively small a change. Cheers, Georg
At 09:22 PM 3/28/2006 +0200, Georg Brandl wrote:
Hi,
since I found myself writing "if __name__ == '__main__'" often these days, I wondered whether PEP 299 could be pronounced upon. I'm not proposing putting it into 2.5, but it should be relatively small a change.
A couple of issues that the PEP doesn't address: * How can you write code that runs in multiple Python versions with this? If you use the current idiom to invoke __main__, it's going to get invoked twice. * A module that imports __main__ (using "import __main__") is going to get a TypeError unless the implementation checks that __main__ is not in fact the __main__ module.
On 3/28/06, Georg Brandl
since I found myself writing "if __name__ == '__main__'" often these days, I wondered whether PEP 299 could be pronounced upon. I'm not proposing putting it into 2.5, but it should be relatively small a change.
If you're asking for a quick pronouncement, it's going to be "no". It's not worth the change (in docs, user habits, etc.) and there's nothing particularly broken. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum
On 3/28/06, Georg Brandl
wrote: since I found myself writing "if __name__ == '__main__'" often these days, I wondered whether PEP 299 could be pronounced upon. I'm not proposing putting it into 2.5, but it should be relatively small a change.
If you're asking for a quick pronouncement, it's going to be "no". It's not worth the change (in docs, user habits, etc.) and there's nothing particularly broken.
It might be worth instead adding an option flag to the executable that implies
"from the loaded module, run __main__() with sys.argv as its argument(s)", so
the user can get this behaviour with `python -X somemodule.py`.
Charles
--
-----------------------------------------------------------------------
Charles Cazabon
On 3/28/06, Charles Cazabon
It might be worth instead adding an option flag to the executable that implies "from the loaded module, run __main__() with sys.argv as its argument(s)", so the user can get this behaviour with `python -X somemodule.py`.
You can do "python -x somemodule" as long as somemodule.py uses the if __name__=='__main__' convention. What does your proposal add? -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Sorry, I meant "python -m somemodule".
On 3/28/06, Guido van Rossum
On 3/28/06, Charles Cazabon
wrote: It might be worth instead adding an option flag to the executable that implies "from the loaded module, run __main__() with sys.argv as its argument(s)", so the user can get this behaviour with `python -X somemodule.py`.
You can do "python -x somemodule" as long as somemodule.py uses the if __name__=='__main__' convention. What does your proposal add?
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum
On 3/28/06, Charles Cazabon
wrote: It might be worth instead adding an option flag to the executable that implies "from the loaded module, run __main__() with sys.argv as its argument(s)", so the user can get this behaviour with `python -X somemodule.py`.
You can do "python -m somemodule" as long as somemodule.py uses the if __name__=='__main__' convention. What does your proposal add?
Well, it's not really my proposal. I'm just suggesting the behaviour in
PEP299 could be added via a commandline option so that it doesn't introduce
backward-incompatibility.
I don't see any particular benefit to PEP299 myself -- I'd rather just add an
entry to the style guide about standardizing the "executable stanza" and main() function.
Charles
--
-----------------------------------------------------------------------
Charles Cazabon
OK. -1 on PEP 299 it is.
On 3/28/06, Charles Cazabon
Guido van Rossum
wrote: On 3/28/06, Charles Cazabon
wrote: It might be worth instead adding an option flag to the executable that implies "from the loaded module, run __main__() with sys.argv as its argument(s)", so the user can get this behaviour with `python -X somemodule.py`.
You can do "python -m somemodule" as long as somemodule.py uses the if __name__=='__main__' convention. What does your proposal add?
Well, it's not really my proposal. I'm just suggesting the behaviour in PEP299 could be added via a commandline option so that it doesn't introduce backward-incompatibility.
I don't see any particular benefit to PEP299 myself -- I'd rather just add an entry to the style guide about standardizing the "executable stanza" and main() function.
Charles -- ----------------------------------------------------------------------- Charles Cazabon
GPL'ed software available at: http://pyropus.ca/software/ ----------------------------------------------------------------------- _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum wrote:
On 3/28/06, Charles Cazabon
wrote: It might be worth instead adding an option flag to the executable that implies "from the loaded module, run __main__() with sys.argv as its argument(s)", so the user can get this behaviour with `python -X somemodule.py`.
You can do "python -x somemodule" as long as somemodule.py uses the if __name__=='__main__' convention. What does your proposal add?
FWIW, you can already (Python 2.4) do something like: ------- x.py --------- import sys if __name__ == __main__: del sys.argv[0] # Get rid of reference to ourselves mod_name = sys.argv[0] # First arg is now module to be run mod = __import__(mod_name) # Run the top level module code main = mod.__main__ # Grab the main function sys.modules["__main__"] = mod # Make that module the __main__ one try: sys.argv[0] = mod.__file__ # Try to set argv[0] properly except AttributeError: pass sys.exit(main(*sys.argv)) # Run the function ------------------------- Put that in site-packages and "python -mx somemodule" will do exactly as Charles describes. Getting it to work with a filename instead of a module name would be a bit trickier, but not a lot. However, I think PEP 299 is mainly a holdover from C/C++/Java where the top level of a module is a play area for the compiler that the runtime never really gets to see. I know I found PEP 299 appealing when I first seriously started using Python, but the appeal faded over time as I got used to the idea of being able to have control logic at the top level of a module (to the point where the idea is now thoroughly *un*appealing). PEP 299's other 'use case' (trying to run another program's main function from within the current program) seems like a recipe for disaster - far better to use the subprocess module instead (since, strangely enough, application initialisation code has this tendency to assume it has sole control of the interpreter). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org
Die, thread. Do I personally have to go into svn and reject this PEP? -- --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (6)
-
Brett Cannon
-
Charles Cazabon
-
Georg Brandl
-
Guido van Rossum
-
Nick Coghlan
-
Phillip J. Eby