title: Python inheritance: convert from Base class to Derived class
body: <p>I'm still pretty new to Python, so bear with me.  Here is my problem:</p>
  
  <p>I have a base class, lets call it a sports game:</p>
  
  <pre><code>class Game:
      def __init__(self):
          self.home_team = None
          self.away_team = None
  </code></pre>
  
  <p>and I have multiple derived classes for each sport, but lets use baseball as an example:</p>
  
  <pre><code>class BaseballGame(Game):
      def __init__(self):
          self.home_pitcher = None
          self.away_pitcher = None
  </code></pre>
  
  <p>So far everything is good.  However I have another utility function in a separate python module which will generate and populate a list of all the games being played on a given day for that sport.</p>
  
  <pre><code>def fetch_game_data:
      games = []
      games_found_online = code_that_fetches_online_games
      for online_game in games_found_online:
          new_game = Game()
          new_game.home_team = ...
          new_game.away_team = ...
          games.append(new_game)
      return games
  </code></pre>
  
  <p>Its obviously much more complicated than this with a lot of parsing using BeautifulSoup, but you get the point.  My problem is that this function returns a list of the Base class, but I need a list of the Derived class.  The derived classes will be the ones calling this function to populate a list and operate on it.  The way I see it, I have two options:</p>
  
  <ol>
  <li>I could implement a dreaded circular dependency and have the fetch_game_data function know about all of the derived classes and call the derived class constructors instead of the base class constructors.  The derived classes already need to import the fetch_data module, but now the fetch_data module will have to import all of the derived classes to know about their constructors.  And what makes it worse is that the fetch_data module won't have any need to touch any of the derived class fields - it only populates base class fields.  The circular dependency is JUST so I can create the objects.
  <ol start="2">
  <li>I could implement code that downcasts a Base class Game to a Derived class Game (like BaseballGame).  Then, when the fetch_game_data function returns all of the games I can just convert them all to a Derived class object and continue on my way.  Unfortunately I haven't seen much in the way of how to implement this.  I tried just changing the <strong>class</strong> variable, but then the code complains because Derived class variables don't exist.</li>
  </ol></li>
  </ol>
  
  <p>Another option I considered, but quickly fell apart, was to send an existing list of derived class objects into the fetch_game_data function, and instead of creating new Game objects it would just populate existing ones.  The problem is that I won't know how many game objects I need.  The fetch_game_data function determines how many games are needed by parsing the webpage.  I suppose I could send in the max number of games but using number_of_teams/2, but what if there is a double-header in baseball?  This quickly falls apart.  I suppose I could write a function that will fetch the game data and just return the number of games for the day.  Then I could populate a list of Derived games that size and send it in to be populated.  But I would have to fetch all the webpage data AGAIN and parse it AGAIN to populate the list.</p>
  
  <p>Nothing but bad options!  I'm hoping there is a simple and elegant solution that has just eluded me thus far.  I'm open to any suggestions, including a redesign if it makes sense.</p>
  
  <p>Thanks!</p>