hasattr() vs try-except block to deal with non-existent attributes

Any benches that illustrate difference in performance? timeit it’s your friend $ python -mtimeit -s ‘class C(object): a = 4 c = C()’ ‘hasattr(c, “nonexistent”)’ 1000000 loops, best of 3: 1.87 usec per loop $ python -mtimeit -s ‘class C(object): a = 4 c = C()’ ‘hasattr(c, “a”)’ 1000000 loops, best of 3: 0.446 usec … Read more