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']
@srinivas-adivi
Copy link

# I didn't get your meaning of "invalid" but
# To make it simple, above scenarios are as same as below
print "a is not None where a = 0?", 0 is not None
print "b is not None where b = 1?", 1 is not None
print "d is not None where d = None?", None is not None
# Check below statement for "valid" or not(w.r.t your meaning of 'invalid') and Let me know if you need any clarification.
print "c is not None where c = '' ", '' is not None

@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