To really understand the performance of your system, you will need to profile it. But Python is a language that encourages the use of such exceptions, so they have no unusual overhead, as in some other languages.
For example, sometimes people discuss this choice:
if hasattr(obj, "attr"): use(obj.attr) else: some_other(obj)
or
try: use(obj.attr) except AttributeError: some_other(obj)
People who say that you should use the first to avoid an exception should understand that internally, hasattr is implemented by accessing the attribute and returning False if an AttributeError occurs. Thus, in fact, both code fragments use exceptions.
Ned batchelder
source share