a = SomeClass()
if hasattr(a, 'property'):
a.property
Is this the only way to check if there is a property or not? Is there any other way to do the same thing?
You could just use the property and catch the AttributeError
exception if it doesn't exist. But using hasattr
is also a reasonable approach.
A potential issue with catching the exception is that you can't easily distinguish between the attribute not existing, and it existing but when you call it some code is run and that code raises an AttributeError
(perhaps due to a bug in the code).
You may also want to look at these related questions for more information on this topic:
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