Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between hasattr() and 'attribute' in dir()?

Tags:

python

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?

like image 681
augustomen Avatar asked Jul 18 '13 12:07

augustomen


People also ask

What is Hasattr () used for in python?

Python hasattr() Function The hasattr() function returns True if the specified object has the specified attribute, otherwise False .

What is the Hasattr used for?

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.

Which method will ensure that an attribute has been defined before you access it Hasattr Hasattr () Hasattr _( Hasattr?

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.

When should you use Hasattr obj name )?

What is hasattr(obj,name) used for? Explanation: hasattr(obj,name) checks if an attribute exists or not and returns True or False.


2 Answers

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.

like image 62
Martijn Pieters Avatar answered Sep 30 '22 15:09

Martijn Pieters


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
like image 31
jcr Avatar answered Sep 30 '22 15:09

jcr