How do I check if an object has some attribute? For example:
>>> a = SomeClass()
>>> a.property
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: SomeClass instance has no attribute 'property'
How do I tell if a
has the attribute property
before using it?
hasattr()
returns) or is it ok to throw an AttributeError
exception? - anyone property
decorator and builtin too, and those are slightly different than normal attributes/methods. - anyone Try hasattr()
:
if hasattr(a, 'property'):
a.property
See zweiterlinde's answer below, who offers good advice about asking forgiveness! A very pythonic approach!
The general practice in python is that, if the property is likely to be there most of the time, simply call it and either let the exception propagate, or trap it with a try/except block. This will likely be faster than hasattr
. If the property is likely to not be there most of the time, or you're not sure, using hasattr
will probably be faster than repeatedly falling into an exception block.
Answered 2023-09-20 20:31:48
import string hasattr(string, "lower")
- anyone hasattr
is exactly the same as using try
/except AttributeError
: the docstring of hasattr (in Python 2.7) says that it uses getattr hand catches exceptions. - anyone hasattr
is unfortunately not exactly the same as a try: ... except AttributeError:
in Python 2.x since hasattr
will catch all exception. Please see my answer for an example and a simple workaround. - anyone self
? like if not hasattr(self, 'property')
inside the __init__()
of the object creation - anyone As Jarret Hardie answered, hasattr
will do the trick. I would like to add, though, that many in the Python community recommend a strategy of "easier to ask for forgiveness than permission" (EAFP) rather than "look before you leap" (LBYL). See these references:
EAFP vs LBYL (was Re: A little disappointed so far)
EAFP vs. LBYL @Code Like a Pythonista: Idiomatic Python
ie:
try:
doStuff(a.property)
except AttributeError:
otherStuff()
... is preferred to:
if hasattr(a, 'property'):
doStuff(a.property)
else:
otherStuff()
Answered 2023-09-20 20:31:48
try:
should be the attempted attribute access; there's no reason to wrap the execution of doStuff
as well. There still is some potential for ambiguity though: if property
is a computed property instead of a plain attribute, its implementation could raise AttributeError
internally. This is why in almost all real situations like this, getattr
is preferable to either hasattr
or catching AttributeError
. - anyone You can use hasattr()
or catch AttributeError
, but if you really just want the value of the attribute with a default if it isn't there, the best option is just to use getattr()
:
getattr(a, 'property', 'default value')
Answered 2023-09-20 20:31:48
I think what you are looking for is hasattr. However, I'd recommend something like this if you want to detect python properties-
try:
getattr(someObject, 'someProperty')
except AttributeError:
print "Doesn't exist"
else
print "Exists"
The disadvantage here is that attribute errors in the properties __get__
code are also caught.
Otherwise, do-
if hasattr(someObject, 'someProp'):
#Access someProp/ set someProp
pass
Docs:http://docs.python.org/library/functions.html
Warning:
The reason for my recommendation is that hasattr doesn't detect properties.
Link:http://mail.python.org/pipermail/python-dev/2005-December/058498.html
Answered 2023-09-20 20:31:48
hasattr
detects properties in general just fine. It's just that it treats raising an exception in the property
-wrapped function as meaning no such attribute exists; the linked Python mailing list post is about a property that raises an exception when you attempt to access it. For all practical purposes, said attribute doesn't exist, because it will never produce a value. Also, hasattr
only suppresses exceptions in general on Py 3.1 and earlier; in 3.2+, it only suppresses (replacing with False
return) AttributeError
. - anyone According to pydoc, hasattr(obj, prop) simply calls getattr(obj, prop) and catches exceptions. So, it is just as valid to wrap the attribute access with a try statement and catch AttributeError as it is to use hasattr() beforehand.
a = SomeClass()
try:
return a.fake_prop
except AttributeError:
return default_value
Answered 2023-09-20 20:31:48
hasattr
when SomeClass
overrides __getattr__
since hasattr
will catch all exceptions in Python 2.x, not just AttributeError
like you would expect. This was fixed in Python 3.2 — please see my other answer for a simple workaround. - anyone I would like to suggest avoid this:
try:
doStuff(a.property)
except AttributeError:
otherStuff()
The user @jpalecek mentioned it: If an AttributeError
occurs inside doStuff()
, you are lost.
Maybe this approach is better:
try:
val = a.property
except AttributeError:
otherStuff()
else:
doStuff(val)
Answered 2023-09-20 20:31:48
hasattr()
is the right answer. What I want to add is that hasattr()
can be used well in conjunction with assert (to avoid unnecessary if
statements and make the code more readable):
assert hasattr(a, 'property'), 'object lacks property'
print(a.property)
In case that the property is missing, the program will exit with an AssertionError
and printing out the provided error message (object lacks property
in this case).
As stated in another answer on SO:
Asserts should be used to test conditions that should never happen. The purpose is to crash early in the case of a corrupt program state.
Often this is the case when a property is missing and then assert
is very appropriate.
Answered 2023-09-20 20:31:48
hasattr
not the surrounding code. - anyone For objects other than dictonary:
if hasattr(a, 'property'):
a.property
For dictionary, hasattr()
will not work.
Many people are telling to use has_key()
for dictionary, but it is depreciated.
So for dictionary, you have to use has_attr()
if a.has_attr('property'):
a['property']
Or you can also use
if 'property' in a:
Answered 2023-09-20 20:31:48
if 'property' in a:
- anyone EDIT:This approach has serious limitation. It should work if the object is an iterable one. Please check the comments below.
If you are using Python 3.6 or higher like me there is a convenient alternative to check whether an object has a particular attribute:
if 'attr1' in obj1:
print("attr1 = {}".format(obj1["attr1"]))
However, I'm not sure which is the best approach right now. using hasattr()
, using getattr()
or using in
. Comments are welcome.
Answered 2023-09-20 20:31:48
in
keyword works for checking iterable types. For example, 'foo' in None
throws the error TypeError: argument of type 'NoneType' is not iterable
. The fix is to check if the type is iterable before using in
. After correcting for edge cases like non-iterable type, you're probably better off using hasattr()
because it's designed to handle the edge cases. - anyone dict
as a "lightweight object", similar to the design of JavaScript objects, but most normal classes will not support this in general (getting a variant on the error mentioned by @SethDifley). - anyone Hope you expecting hasattr(), but try to avoid hasattr() and please prefer getattr(). getattr() is faster than hasattr()
using hasattr():
if hasattr(a, 'property'):
print a.property
same here i am using getattr to get property if there is no property it return none
property = getattr(a,"property",None)
if property:
print property
Answered 2023-09-20 20:31:48
getattr() is faster than hasattr()
, could you please comment on this? Why faster? - anyone False
you can end up with a false positive. - anyone Here's a very intuitive approach :
if 'property' in dir(a):
a.property
If a is a dictionary, you can check normally
if 'property' in a:
a.property
Answered 2023-09-20 20:31:48
Depending on the situation you can check with isinstance
what kind of object you have, and then use the corresponding attributes. With the introduction of abstract base classes in Python 2.6/3.0 this approach has also become much more powerful (basically ABCs allow for a more sophisticated way of duck typing).
One situation were this is useful would be if two different objects have an attribute with the same name, but with different meaning. Using only hasattr
might then lead to strange errors.
One nice example is the distinction between iterators and iterables (see this question). The __iter__
methods in an iterator and an iterable have the same name but are semantically quite different! So hasattr
is useless, but isinstance
together with ABC's provides a clean solution.
However, I agree that in most situations the hasattr
approach (described in other answers) is the most appropriate solution.
Answered 2023-09-20 20:31:48
You can use hasattr() to check if object or class has an attribute in Python.
For example, there is Person
class as shown below:
class Person:
greeting = "Hello"
def __init__(self, name, age):
self.name = name
self.age = age
def test(self):
print("Test")
Then, you can use hasattr() for object as shown below:
obj = Person("John", 27)
obj.gender = "Male"
print("greeting:", hasattr(obj, 'greeting'))
print("name:", hasattr(obj, 'name'))
print("age:", hasattr(obj, 'age'))
print("gender:", hasattr(obj, 'gender'))
print("test:", hasattr(obj, 'test'))
print("__init__:", hasattr(obj, '__init__'))
print("__str__:", hasattr(obj, '__str__'))
print("__module__:", hasattr(obj, '__module__'))
Output:
greeting: True
name: True
age: True
gender: True
test: True
__init__: True
__str__: True
__module__: True
And, you can also use hasattr() directly for class name as shown below:
print("greeting:", hasattr(Person, 'greeting'))
print("name:", hasattr(Person, 'name'))
print("age:", hasattr(Person, 'age'))
print("gender:", hasattr(Person, 'gender'))
print("test:", hasattr(Person, 'test'))
print("__init__:", hasattr(Person, '__init__'))
print("__str__:", hasattr(Person, '__str__'))
print("__module__:", hasattr(Person, '__module__'))
Output:
greeting: True
name: False
age: False
gender: False
test: True
__init__: True
__str__: True
__module__: True
Answered 2023-09-20 20:31:48
This is super simple, just use dir(
object)
This will return a list of every available function and attribute of the object.
Answered 2023-09-20 20:31:48
You can check whether object
contains attribute by using hasattr
builtin method.
For an instance if your object is a
and you want to check for attribute stuff
>>> class a:
... stuff = "something"
...
>>> hasattr(a,'stuff')
True
>>> hasattr(a,'other_stuff')
False
The method signature itself is hasattr(object, name) -> bool
which mean if object
has attribute which is passed to second argument in hasattr
than it gives boolean True
or False
according to the presence of name
attribute in object.
Answered 2023-09-20 20:31:48
Another possible option, but it depends if what you mean by before:
undefined = object()
class Widget:
def __init__(self):
self.bar = 1
def zoom(self):
print("zoom!")
a = Widget()
bar = getattr(a, "bar", undefined)
if bar is not undefined:
print("bar:%s" % (bar))
foo = getattr(a, "foo", undefined)
if foo is not undefined:
print("foo:%s" % (foo))
zoom = getattr(a, "zoom", undefined)
if zoom is not undefined:
zoom()
output:
bar:1
zoom!
This allows you to even check for None-valued attributes.
But! Be very careful you don't accidentally instantiate and compare undefined
multiple places because the is
will never work in that case.
because of what I was warning about in the above paragraph, having multiple undefineds that never match, I have recently slightly modified this pattern:
undefined = NotImplemented
NotImplemented
, not to be confused with NotImplementedError
, is a built-in: it semi-matches the intent of a JS undefined
and you can reuse its definition everywhere and it will always match. The drawbacks is that it is "truthy" in booleans and it can look weird in logs and stack traces (but you quickly get over it when you know it only appears in this context).
Answered 2023-09-20 20:31:48