Reputation: 19329
While working with multiple conditions I am currently using following syntax:
if hasattr(myClass,methodA)==False or hasattr(myClass,methodB)==False or hasattr(myClass,methodC)==False: return
I wonder if there is a shorter way of doing the same. What I don't like particularly is that I have to use "==False" three times in repetition. Would it be correct if used this instead:
if not hasattr(myClass,methodA) or not hasattr(myClass,methodB) or not hasattr(myClass,methodC): return
Upvotes: 1
Views: 83
Reputation: 251355
You can do if not all(hasattr(myClass,meth) for meth in ('methodA', 'methodB', 'methodC'))
Upvotes: 4
Reputation: 17042
Functionally:
if any(map(lambda method: not hasattr(myClass, method), ['methodA', 'methodB', 'methodC'])):
Upvotes: 0