break in a module

Chris Angelico rosuav at gmail.com
Thu Jun 16 20:57:52 EDT 2011


On Fri, Jun 17, 2011 at 10:48 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Perhaps the most sensible alternative is conditional importing:
>
> # === module extras.py ===
>
> def ham(): pass
> def cheese(): pass
> def salad(): pass
>
>
> # === module other.py ===
>
> def spam(): pass
>
> if not some_condition: from extras import *
>

This would, if I understand imports correctly, have ham() operate in
one namespace and spam() in another. Depending on what's being done,
that could be quite harmless, or it could be annoying (no sharing
module-level constants, etc).

As to which keyword is used, I would be inclined to go with 'return'
rather than 'break'. The module is thus a procedure in its own right.
Of course, that's assuming the feature's actually needed, which isn't
certain by any means.

ChrisA



More information about the Python-list mailing list