Reputation: 360
I need to use python hasattr
for my very specific purpose. I need to check that an object is having an attribute, and not having another attribute.
Consider the class object named model
, I need to check that whether it is having an attribute called domain_id
:
if hasattr(model, 'domain_id'):
I also need to check for one more condition that it shouldn't have attribute called type
.
if not hasattr(model, 'type'):
How to combine the two checks here?
Upvotes: 4
Views: 8290
Reputation: 1121446
Just combine the two conditions with and
:
if hasattr(model, 'domain_id') and not hasattr(model, 'type'):
The if
block will only execute if both conditions are true.
Upvotes: 6