module import performance question

Scott D. Daniels daniels at db-x.com
Tue Nov 11 18:11:13 EST 2003


jeff at cowz.com (Jeff Sykes) wrote in message news:<729b56c7.0311111158.3602e7d5 at posting.google.com>...
> ...
> Here's a snip of the block that occured right after import:
> 
> try:
>     # next line was not there before
>     if hasattr(dyn_blocks[key], "BUFFER_ON"):
>         context.setBufferFlag(dyn_blocks[key].BUFFER_ON)
> except:
>     Log.stacktrace(Log.ERROR)
> I guess the lesson I learned is don't use exception handling 
> unnecessarily. Which I know not to do anyway. Sigh. Back to 
> school for me.

Wrong lesson.  Python wants you to use exception handling.
The time sink above code is the unnecessary uses of:
>     Log.stacktrace(Log.ERROR)

A couple of Alternatives:
## smaller, not quite careful enough for my taste
try:
    context.setBufferFlag(dyn_blocks[key].BUFFER_ON)
except AttributeError:
    pass
except:
    Log.stacktrace(Log.ERROR)

## better, more to my taste
try:
    try:
        flag = dyn_blocks[key].BUFFER_ON
    except AttributeError:
        pass
    else:
        context.setBufferFlag(flag)
except:
    Log.stacktrace(Log.ERROR)

-Scott




More information about the Python-list mailing list