[Python-ideas] breaking out of module execution
steve at pearwood.info
Wed Apr 25 02:55:07 CEST 2012
M.-A. Lemburg wrote:
> Georg Brandl wrote:
>> On 24.04.2012 22:20, M.-A. Lemburg wrote:
>>>> There's a subtle bug here that shows that the proposed feature has its
>>>> awkward points: you probably want to execute the "if __name__ == '__main__'"
>>>> block in the C extension case as well.
>>> No, you don't :-) If you would have wanted that to happen, you'd
>>> put the "if __name__..." into the else: branch.
>> Not sure I understand. Your example code is flawed because it doesn't execute
>> the main() for the C extension case. Of course you can duplicate the code in
>> the else branch, but you didn't do it in the first place, which was the bug.
> Ok, you got me :-) Should've paid more attention.
I think you have inadvertently demonstrated that that this proposed feature is
hard to use correctly. Possibly even harder to use than existing idioms for
solving the problems this is meant to solve. If the user does use it, they
will likely need to duplicate code, which encourages copy-and-paste programming.
Even if break at the module level is useful on rare occasions, I think the
usefulness is far outweighed by the costs:
- hard to use correctly, hence code using this feature risks being buggy
- encourages premature micro-optimization, or at least the illusion of
- encourages or requires duplicate code and copy-and-paste programming
- complicates the top-level program flow
Today, if you successfully import a module, you know that all the top-level
code in that module was executed. If this feature is added, you cannot be sure
what top-level code was reached unless you scan through all the code above it.
In my opinion, this is an attractive nuisance.
-1 on the feature.
More information about the Python-ideas