On 2012-12-08 07:17, Chris Angelico wrote:
On Sat, Dec 8, 2012 at 6:01 PM, Terry Reedy <tjre...@udel.edu> wrote:
Unfortunately, catching exceptions may be and often is as slow as the
redundant check and even multiple redundant checks.

It depends on how often you're going to catch and how often just flow
through. In Python, as in most other modern languages, exceptions only
cost you when they get thrown. The extra check, though, costs you in
the normal case.

That's where the .get method comes in handy:

MISSING = object()
...
value = my_dict.get(key, MISSING)
if value is not MISSING:
   ...

It could be faster if the dict often doesn't contain the key.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to