Which idiom is better? try: ... except Attr..Err or hasattr?

Alexander Williams thantos at chancel.org
Thu Jan 27 04:27:03 EST 2000


On Thu, 27 Jan 2000 08:40:07 +0100 (MET), Peter Funk
<pf at artcom-gmbh.de> wrote:

>Which coding idiom is better?  The second form is one line shorter than

Personally, I prefer the former idiom with try/except; some object
because its slightly slower than the other, but I find it clearer.

Perhaps most important from an efficiency PoV is that the try/except
structure really implies you expect the except clause to /really/ be
the uncommon case, a true exception.  If it happens roughly 50% of the
time, the if hasattr() construct may be more what you want in terms of
intent.  If its the general case, reverse the sense of your try.  :)

-- 
Alexander Williams (thantos at gw.total-web.net)           | In the End,
  "Join the secret struggle for the soul of the world." | Oblivion
  Nobilis, a new Kind of RPG                            | Always
  http://www.chancel.org                                | Wins



More information about the Python-list mailing list