Get Attributes python
<pre><code>class A(object):
      a = 1
      b = 0
      c = None
      d = None
a_obj=A()
a_list = ['a', 'b', 'c', 'd']
attrs_present = filter(lambda x: getattr(a_obj, x), a_list)
</code></pre>

<p>I want both a and b attributes, here 0 is a valid value. I don't want to use comparison==0</p>

<p>is there a way to get those?
Any help will be appriciated, Thanks.</p>

<p>If you want to exclude <code>c</code>, <code>d</code> (<code>None</code>s), use <code>is None</code> or <code>is not None</code>:</p>

<pre><code>attrs_present = filter(lambda x: getattr(a_obj, x, None) is not None, a_list)
# NOTE: Added the third argument `None`
#       to prevent `AttributeError` in case of missing attribute 
#       (for example, a_list = ['a', 'e'])
</code></pre>

<p>If you want to include <code>c</code>, <code>d</code>, use <a href="https://docs.python.org/2/library/functions.html#hasattr" rel="nofollow"><code>hasattr</code></a>:</p>

<pre><code>attrs_present = filter(lambda x: hasattr(a_obj, x), a_list)
</code></pre>

python python-2.7