What's the difference between hasattr() and 'attribute' in dir()?
python
Solution
It is not quite the same thing. `dir()` is a diagnostic tool that omits attributes that `getattr()` and `hasattr()` would find.
From the `dir()` documentation:
The default `dir()` mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:
- If the object is a module object, the list contains the names of the module’s attributes.
- If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
- Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.
and
Note: Because `dir()` is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, `metaclass` attributes are not in the result list when the argument is a class.
Emphasis mine.
This means that `hasattr()` will find metaclass supplied attributes, but `dir()` would not, and what is found can differ accross Python releases as the definition for the function is to provide debugging convenience, not completeness.
Demo of the specific metaclass scenario, where `hasattr()` finds the metaclass-defined attribute:
>>> class Meta(type):
... foo = 'bar'
...
>>> class Foo(metaclass=Meta):
... pass
...
>>> hasattr(Foo, 'foo')
True
>>> 'foo' in dir(Foo)
False
Last but not least:
If the object has a method named `__dir__()`, this method will be called and must return the list of attributes.
This means that `hasattr()` and `dir()` can vary even more widely in what attributes are 'found' if a `.__dir__()` method has been implemented.
Just stick with `hasattr()`. It is faster, for one, because testing for an attribute is cheap as that's just a membership test against one or more dictionaries. Enumerating all dictionary keys and merging them across instance, class and base classes on the other hand has a far higher CPU cost.
Problem
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?