Confused compare function :)

MRAB python at mrabarnett.plus.com
Sat Dec 8 12:50:46 EST 2012


On 2012-12-08 07:17, Chris Angelico wrote:
> On Sat, Dec 8, 2012 at 6:01 PM, Terry Reedy <tjreedy at 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.



More information about the Python-list mailing list