For example:

>>> s = 'string'
>>> hasattr(s, 'join')
True
>>> 'join' in dir(s)
True

Python documentation says that hasattr is implemented calling getattr and seeing whether it raises an exception or not. However, that leads to a great overhead, since the value obtained is discarded and an exception may be raised.

The question is if calling 'attribute' in dir(obj) means the same thing, is it faster, safe, or may it fail in a particular occasion?

Edit
Report