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?
Python hasattr() Function The hasattr() function returns True if the specified object has the specified attribute, otherwise False .
Definition. Python hasattr() function is used to return value TRUE after checking whether the object has the given attribute, else return FALSE. The Python hasattr() built-in function is used to check if the specified object has the named attribute present within a class or not.
Python hasattr() method Python hasattr() function is an inbuilt utility function, which is used to check if an object has the given named attribute and return true if present, else false.
What is hasattr(obj,name) used for? Explanation: hasattr(obj,name) checks if an attribute exists or not and returns True or False.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With