Reputation: 385
I'm using python and calling an api request. Not all the tweets have quoted_status field since not all of them are quoted. How can I overcome the error
AttributeError: 'Status' object has no attribute 'quoted_status'
and print for example 'null' if the quoted_status is not available?
I'm working in a loop, my actual code is this:
for status in timeline:
print status.quoted_status
I tried also with except but with no success.
Upvotes: 3
Views: 3523
Reputation: 10631
You can check whether the object has the attribute with hasattr
keyword.
for status in timeline:
if hasattr(status, 'quoted_status'):
print (status.quoted_status)
else:
print ("null")
hasattr(object, name)
The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)
Upvotes: 4