I Know what the error:
AttributeError: 'NoneType' object has no attribute 'something'
means, but what I cannot figure out is how to catch it. Basically i want to check to see if my object is created or not.
I want to do something like this:
try:
test = self.myObject.values()
except:
print "Error happened"
But every time I do I get:
AttributeError: 'NoneType' object has no attribute 'values'
Can someone tell me how to catch this?
you can use the hasattr(object, name) method which returns True or False. If you try to access an attribute which does not exist you will always receive an exception. https://docs.python.org/2/library/functions.html#hasattr
If you still wish to catch the exception then simply do a try/catch block with exception type 'AttributeError'
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