Skip to content

Instantly share code, notes, and snippets.

@karnikamit
Last active October 7, 2016 09:26
Show Gist options
  • Save karnikamit/2052f60ba2b23b38f9d5b24f68b9dfc6 to your computer and use it in GitHub Desktop.
Save karnikamit/2052f60ba2b23b38f9d5b24f68b9dfc6 to your computer and use it in GitHub Desktop.
"""
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']
@karnikamit
Copy link
Author

@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