9

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?

augustomen
  • 8,977
  • 3
  • 43
  • 63

4 Answers4

17

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    A drawback of using `hasattr` is that it will trigger properties execution, which may be costy. In cases such as a Django `OneToOneField` relations, this will even trigger a database query. I don't suppose there is an alternative, is there? – augustomen Jul 18 '13 at 13:21
  • @AugustoMen: You can use *application specific* means of testing for an attribute being present. I'm sure Django has a means of enumerating fields, for example. – Martijn Pieters Jul 18 '13 at 13:22
7

the hasattr is more than 100 times faster :)

In [137]: s ='string'

In [138]: %timeit hasattr(s, 'join')
10000000 loops, best of 3: 157 ns per loop

In [139]: %timeit 'join' in dir(s)
100000 loops, best of 3: 19.3 us per loop
jcr
  • 1,015
  • 6
  • 18
1

dir() does not call getattr(), or anything like that, It depends on the class to "describe" itself:

>>> class Foo(object):
...     def __dir__(self):
...         return ['apples', 'bananas', 'mangoes']
...     def __getattr__(self, attr):
...         return {'a': 1}[attr]
...     
>>> foo = Foo()
>>> hasattr(foo, 'a')
True
>>> hasattr(foo, 'apples')
False
>>> 'a' in dir(foo)
False
>>> 'apples' in dir(foo)
True

You should only use dir() when looking for documentation.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
1

hasattr() is basically

try:
    s.attribute
    return True
except AttributeError:
    return False

while "attribute in dir(s)" is more like:

for attr in dir(s):
    if attribute == attr:
        return True
return False

Hence, hasattr is expected to be a bit faster.

Anyways, if I be allowed to go a bit off-track then I would suggest this. If you want to do something like:

if hasattr(s, 'attributeName'):
    s.attributeName()
else:
    do_stuff()

then its advisable to do it like this:

try:
    s.attributeName()
except AttributeError:
    do_stuff()

Why?

  1. To avoid the overhead of an extra try-except block/ for loop.
  2. In python, it's Easier to Ask Forgiveness than Permission.
Sudipta
  • 4,773
  • 2
  • 27
  • 42