I was reading about hasattr, there I saw one motto in Python that says that is Easier to ask for forgiveness than permission where I normally agree.
I tried to do a performance test in this case with a python code:
import timeit
definition="""\
class A(object):
a = 1
a = A()
"""
stm="""\
hasattr(a, 'a')
"""
print timeit.timeit(stmt=stm, setup=definition, number=10000000)
stm="""\
getattr(a, 'a')
"""
print timeit.timeit(stmt=stm, setup=definition, number=10000000)
With the results:
$ python test.py
hasattr(a, 'a')
1.26515984535
getattr(a, 'a')
1.32518696785
With this, I analyzed that the getattr is slower than hasattr, but in the official documentation, it says that it calls getattr.
So why in the documentation we say that hasattr calls getattr and we seem to inspire the users to use getattr instead of hasattr when it really isn´t due to performance?