Last active
October 7, 2016 09:26
-
-
Save karnikamit/2052f60ba2b23b38f9d5b24f68b9dfc6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Get both a and b attributes, here 0 is a valid value. | |
Don't use comparison==0 | |
What if c = '', its invalid! | |
""" | |
class A(object): | |
a = 1 | |
b = 0 | |
c = '' | |
d = None | |
a_obj = A() | |
a_list = ['a', 'b', 'c', 'd'] | |
if __name__ == '__main__': | |
filtered_list = filter(lambda x: getattr(a_obj, x, None) is not None, a_list) | |
print filtered_list # ['a', 'b', 'c'] |
srinivas-adivi
commented
Sep 29, 2016
@srinivas-adivi
- 0 is valid, i.e if a = 0 then its Boolean value must return True. Thus it must be included in the filtered list
- '' is invalid, i.e if a = '' then its Boolean value must return False. Thus it shouldn't be included in the filtered list.
- Hope I made it clear
Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment