title: What is a better way to iterate over dataclass keys and values?
body: <p>I have two dataclasses, <code>Route</code> and <code>Factors</code>. <code>Route</code> contains a value and three copies of <code>Factors</code>.</p>
  <p><code>Route</code> does not know how many variables <code>Factors</code> contains. I want to get the name of these variables, and then get the respective value of each one, for each copy of <code>Factors</code>.</p>
  <p>Here is what I currently have:</p>
  <pre><code>@dataclass
  class Factors:
      do: bool  # does it do the route
      hub: int # how many of the locations are hubs
  
      def __init__(self, do_init):
          self.do = do_init
          self.hub = 0 # will add later 
  
      def __str__(self):
          return &quot;%s&quot; % self.do
  
  
  @dataclass
  class Route:
      route: tuple
      skyteam: Factors
      star: Factors
      oneworld: Factors
  
      def __init__(self, route):
          self.route = route.get('route')
          # this could probably be done with one line loop and a variable with names
          self.skyteam = Factors(route.get('skyteam'))
          self.star = Factors(route.get('star'))
          self.oneworld = Factors(route.get('oneworld'))
  
      def __str__(self):
          table = [[self.route, &quot;SkyTeam&quot;, &quot;StarAlliance&quot;, &quot;OneWorld&quot;]] # var name is fine
          for var in Factors.__dict__.get('__annotations__').keys():  # for each factor
              factor = [var]
              factor.append(self.skyteam.__dict__.get(var))
              factor.append(self.star.__dict__.get(var))
              factor.append(self.oneworld.__dict__.get(var))
              table.append(factor)
          return tabulate.tabulate(table, tablefmt='plain')
  </code></pre>
  <p>Input is</p>
  <pre><code>{'route': ('BOS', 'DXB'), 'skyteam': True, 'star': True, 'oneworld': True}
  </code></pre>
  <p>Current output is</p>
  <pre class="lang-none prettyprint-override"><code>('BOS', 'DXB')  SkyTeam  StarAlliance  OneWorld
  do              True     True          True
  hub             0        0             0
  </code></pre>
  <p>Maybe I could search <code>Route</code> for each variable that contains a <code>Factors</code> datatype and iterate over those?</p>