[Python-ideas] PEP for executing a module in a package containing relative imports (original) (raw)
Neil Toronto ntoronto at cs.byu.edu
Mon Apr 23 01:51:26 CEST 2007
- Previous message: [Python-ideas] PEP for executing a module in a package containing relative imports
- Next message: [Python-ideas] PEP for executing a module in a package containing relative imports
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Steven Bethard wrote:
On 4/22/07, Jim Jewett <jimjjewett at gmail.com> wrote:
# Equivalent to today if name == sys.modules["main"].name:
# Better than today if name is sys.modules["main"].name: # What I would like (pending PEP I hope to write tonight) if thismodule is sys.modules["main"]: Is it just me, or are the proposals starting to look more and more like:: public static void main(String args[]) I think this PEP now needs to explicitly state that keeping the "am I the main module?" idiom as simple as possible is not a goal. Because everything I've seen (except for the original proposals in the PEP) are substantially more complicated than the current:: if name == 'main': I guess I don't understand why we wouldn't be willing to put up with a new module attribute or builtin to minimize the boilerplate in pretty much every Python application out there.
Agreed - it's getting horrid. As Pythonic as they think this is, they're completely forgetting the newb.
So let's look at it from his point of view. Say I'm a Python newb. I've written some modules and some executable Python scripts and I'm somewhat comfy with the language. (Of course, it only took me about two hours to get comfy - this is Python, after all.) I now want to write either:
- A module that runs unit tests when it is run as a script, but not when it's just imported; or
- A script that can be imported as a module when I need a few of its functions. (I should really split them into another module, but this is a use case.)
Now I have to import sys? Never seen that one... okay. Imported. Now, what's this Greek I have to write to test whether the script is the main script? How am I supposed to remember this? This is worse than fork()!
On the other hand, IMNSHO, either of the following two are just about perfect in terms of understandability, and parsimony:
def __main__(): # we really don't need args here
# stuff
if __main__:
# stuff
Chances are, the first will be very familiar, but refreshing that it's just a plain old, gibberish-free function. Both are easier than what we've got currently. (IMO, the first is better, because 1) the code can be put anywhere in the module; 2) it automatically doesn't pollute the global namespace; and 3) it's less boilerplate for complex modules and no more boilerplate for simple ones.)
FWIW, I don't see a problem with a sys.modules['main'] - it would even occasionally be useful - but nobody should be required to use an abomination like that for what's clearly a newbie task: determining whether a module is run as a script.
Neil
- Previous message: [Python-ideas] PEP for executing a module in a package containing relative imports
- Next message: [Python-ideas] PEP for executing a module in a package containing relative imports
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]