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

2019-01-10 09:27发布

if hasattr(obj, 'attribute'):
    # do somthing

vs

try:
    # access obj.attribute
except AttributeError, e:
    # deal with AttributeError

Which should be preferred and why?

12条回答
孤傲高冷的网名
2楼-- · 2019-01-10 09:38

From a practical point of view, in most languages using a conditional will always be consderably faster than handling an exception.

If you're wanting to handle the case of an attribute not existing somewhere outside of the current function, the exception is the better way to go. An indicator that you may want to be using an exception instead of a conditional is that the conditional merely sets a flag and aborts the current operation, and something elsewhere checks this flag and takes action based on that.

That said, as Rax Olgud points out, communication with others is one important attribute of code, and what you want to say by saying "this is an exceptional situation" rather than "this is is something I expect to happen" may be more important.

查看更多
祖国的老花朵
3楼-- · 2019-01-10 09:40

The first.

Shorter is better. Exceptions should be exceptional.

查看更多
做自己的国王
4楼-- · 2019-01-10 09:43

I'd suggest option 2. Option 1 has a race condition if some other thread is adding or removing the attribute.

Also python has an Idiom, that EAFP ('easier to ask forgiveness than permission') is better than LBYL ('look before you leap').

查看更多
欢心
5楼-- · 2019-01-10 09:44

I almost always use hasattr: it's the correct choice for most cases.

The problematic case is when a class overrides __getattr__: hasattr will catch all exceptions instead of catching just AttributeError like you expect. In other words, the code below will print b: False even though it would be more appropriate to see a ValueError exception:

class X(object):
    def __getattr__(self, attr):
        if attr == 'a':
            return 123
        if attr == 'b':
            raise ValueError('important error from your database')
        raise AttributeError

x = X()
print 'a:', hasattr(x, 'a')
print 'b:', hasattr(x, 'b')
print 'c:', hasattr(x, 'c')

The important error has thus disappeared. This has been fixed in Python 3.2 (issue9666) where hasattr now only catches AttributeError.

An easy workaround is to write a utility function like this:

_notset = object()

def safehasattr(thing, attr):
    return getattr(thing, attr, _notset) is not _notset

This let's getattr deal with the situation and it can then raise the appropriate exception.

查看更多
三岁会撩人
6楼-- · 2019-01-10 09:48

hasattr internally and rapidly performs the same task as the try/except block: it's a very specific, optimized, one-task tool and thus should be preferred, when applicable, to the very general-purpose alternative.

查看更多
Anthone
7楼-- · 2019-01-10 09:53

I would say it depends on whether your function may accept objects without the attribute by design, e.g. if you have two callers to the function, one providing an object with the attribute and the other providing an object without it.

If the only case where you'll get an object without the attribute is due to some error, I would recommend using the exceptions mechanism even though it may be slower, because I believe it is a cleaner design.

Bottom line: I think it's a design and readability issue rather than an efficiency issue.

查看更多
登录 后发表回答