[Python-ideas] breaking out of module execution
M.-A. Lemburg
mal at egenix.com
Tue Apr 24 22:38:14 CEST 2012
Georg Brandl wrote:
> On 24.04.2012 22:20, M.-A. Lemburg wrote:
>
>>>> Think of code like this (let's assume the "break" statement is used
>>>> for stopping module execution):
>>>>
>>>> """
>>>> #
>>>> # MyModule
>>>> #
>>>>
>>>> ### Try using the fast variant
>>>>
>>>> try:
>>>> from MyModule_C_Extension import *
>>>> except ImportError:
>>>> pass
>>>> else:
>>>> # Stop execution of the module code object right here
>>>> break
>>>>
>>>> ### Ah, well, so go ahead with the slow version
>>>>
>>>> import os, sys
>>>> from MyOtherPackage import foo, bar, baz
>>>>
>>>> class MyClass:
>>>> ...
>>>>
>>>> def MyFunc(a,b,c):
>>>> ...
>>>>
>>>> def main():
>>>> ...
>>>>
>>>> if __name__ == '__main__':
>>>> main()
>>>> """
>>>
>>> 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.
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Apr 24 2012)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
2012-04-28: PythonCamp 2012, Cologne, Germany 4 days to go
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
More information about the Python-ideas
mailing list