Unable to successfully call a class string in a for loop
<p>I have a class</p>

<pre><code>class zero:
  top = " __ "
  mid = "|  |"
  bot = "|__|"
</code></pre>

<p>And I want to use a loop to call the different sections.  E.g.:</p>

<pre><code>def printnum(list):
    chunks = ['top','mid','bot']
    print section       # prints  "top"
    print zero.top      # prints  " __ "
    print zero.section  # fails
    for section in chunks:
        string = ''
        for x in list:
            if x == '1':
                string += getattr(one, section)
            elif x == '2':
                string += getattr(two, section)
            etc....
</code></pre>

<p>I must be missing something pretty basic here.  Can I use my loop to call the different parts of my class?</p>

<p>Here's a snippet of intended functionality:</p>

<pre><code>&gt;&gt;Enter the number you would like printed: 21
 __  __
 __||__
 __| __|
</code></pre>

<p>Your <code>zero</code> class doesn't have an attribute called <code>section</code>, what you can do instead is to use <code>getattr</code> which accepts a string (second argument) as the object property to get, like this:</p>

<pre><code>class zero:
  top = " __ "
  mid = "|  |"
  bot = "|__|"

chunks = ['top','mid','bot']
for section in chunks:
    print section       # prints  "top"
    print zero.top      # prints  " __ "
    print getattr(zero, section)
</code></pre>

<p>Which tells python that you want to get the attribute (all the items in your list when you're looping through it) your object <code>zero</code>.</p>

<p><code>getattr</code> also takes a third argument that would be returned if the object doesn't have that property.</p>

<pre><code>getattr(zero, section, 'Not Found')
</code></pre>

python