[Python-ideas] PEP for executing a module in a package containing relative imports (original) (raw)

Jim Jewett jimjjewett at gmail.com
Fri Apr 20 17🔞51 CEST 2007


On 4/19/07, Brett Cannon <brett at python.org> wrote:

... By leaving the _name_ attribute in a module alone and setting a module attribute named _main_ to a true value for the main module (and thus false in all others) ...

Part of me says that you are already proposing the right answer, as these alternatives are just a little too hackish. Still, they are good enough that they should be listed in the PEP, even if only as rejected alternatives.

(1) You could add a builtin main that is false. The real main module would mask it, but no other code would need to change.

Con: Another builtin, and this one wouldn't even make sense as an independent object.

(2) You could special-case the import to use file instead of name when name == "main"

Con: may be more fragile.

(3) You could set name to (an instance of) a funky string subclass that overrides eq.

Con: may be hard to find exactly the right behavior. Examples: What should str(name) do? Maybe main should be the primary value, and split should be overridden?

-jJ



More information about the Python-ideas mailing list