break in a module

Chris Angelico rosuav at gmail.com
Tue Jun 14 21:33:47 EDT 2011


On Wed, Jun 15, 2011 at 10:51 AM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
>  if condition_1:
>      ...
>      return
>  if condition_2:
>      ...
>      return
>
>  # now do my expensive module stuff
>
>  # finally handle being run as a script
>  if __name__ == "__main__":
>      ...
>

The best way I can think of is:


def expensive_stuff_1():
    ...

def expensive_stuff_2():
    ...

if not condition_1:
    expensive_stuff_1()
if not condition_2:
    expensive_stuff_2()


Depending on what exactly you're doing, this might make perfect sense,
or might be a useless indentation level of its own. If the expensive
stuff divides nicely into units, where each unit is governed by one
condition, it might work out well that way; you could use the same
functions to build your 'if __main__' section too.

ChrisA



More information about the Python-list mailing list