title: Python data structure/object to model static multidimensional table
body: <p>I'm just getting back into coding after a few year hiatus and I'm trying to model multi-tiered static forms in a way that lets me grab and perform operations on a specific form level or an entire sub-tree.</p>
  
  <p><strong>Example Form hierarchy:</strong></p>
  
  <ul>
  <li>MyForm
  
  <ul>
  <li>Question 1</li>
  <li>Part 1
  
  <ul>
  <li>Question 1.1</li>
  </ul></li>
  <li>Part 2
  
  <ul>
  <li>Question 2.1</li>
  <li>SubPart 1
  
  <ul>
  <li>Question 2.1.1</li>
  <li>Question 2.1.2</li>
  </ul></li>
  </ul></li>
  <li>Question 2</li>
  </ul></li>
  </ul>
  
  <p>Each Question will have multiple attributes (question text, whether it's a required field, etc.) and Questions can be at any level of the hierarchy.  </p>
  
  <p>I'd like to be able to do something like this:</p>
  
  <pre><code>&gt;&gt;&gt; MyForm.getQuestionObjects()
  [Question1, Question1_1, Question2_1, Question2_1_1, Question2_1_2, Question2]
  
  &gt;&gt;&gt; MyForm.Part2.getQuestionObjects()
  [Question2_1, Question2_1_1, Question2_1_2]
  </code></pre>
  
  <p>and/or stuff like:</p>
  
  <pre><code>&gt;&gt;&gt; # Get questions (return class members)
  &gt;&gt;&gt; MyForm.SubPart1.getQuestions()
  (('2.1.1 text', otherAttributes), ('2.1.2 text', otherAttributes))
  
  &gt;&gt;&gt; # Get questions -- but replace an attribute on 2.1.2
  &gt;&gt;&gt; MyForm.Part2.getQuestions(replace_attr('Question_2_1_2', 'text', 'New text'))
  (('2.1.1 text', otherAttributes), ('New text', otherAttributes))
  </code></pre>
  
  <p>I keep trying to do this with nested/inner classes, which are a big headache and not well-supported in python.  But even if I can figure out a solution using nested classes, I keep wondering whether there's a much better way of storing this form info somewhere to make it easier for non-coders to edit (probably a plain text template), and then loading the data at run-time since it's static and I'll need it in memory quite often.  The form data won't be updated more than say once per month. Regardless how I store the data, I'd like to figure out a good data structure to represent, traverse, and operate on it.</p>
  
  <ul>
  <li>Is there a way to make a tiered-attributes object like this?</li>
  <li>Could I do something like multidimensional named tuples?</li>
  <li>Any other ideas?</li>
  </ul>
  
  <p>Thanks for any comments.</p>