how to use bool

Paul McGuire ptmcg at austin.rr.com
Fri Jan 4 11:46:27 EST 2008


On Jan 3, 10:09 am, tinn... at isbd.co.uk wrote:
>
> With my philosophical programming hat on the first thing I'd say (as a
> fairly beginning python programmer) is "avoid multiple returns from a
> function/method if at all possible".  They breed all sorts of problems
> and errors, in particular if there's any clearing up to do you have to
> do it in lots of places (or you forget it in some places).
>

This conventional wisdom predates the introduction of constructs such
as try-catch-finally.  In fact, you are just lulling yourself into
some false security if you think that that single return at the end of
your method is the only exit point of your routine.  Exceptions can
(and will) happen just about anywhere.

I know, you had your C hat on, but even C++'ers fall into this trap.
I was on a project that cited explicit delete statements during code
reviews as likely memory leaks in the face of exceptions, and each was
to be replaced with auto-cleanup objects such as auto_ptr.  The same
was done for other resource alloc/release pairs, such as locks,
database connections, cursors, etc.  These were looooong-running
server processes, and they ran for months with no memory growth at
all.

If I were to suggest a similar topic for Python code reviews, it would
be to look at paired statements and to ensure that the cleanup/
recovery statement was wrapped in a finally block.

There's more than just memory that needs bookkeeping, so garbage
collection wont solve all these other resource management problems.

-- Paul



More information about the Python-list mailing list