title: Python design question: When dependencies can't be instantiated ahead of time
body: <p>Sorry for the poor title but if I knew the class of problems I was tackling perhaps I could find an answer.</p>
  <p>In an ideal situation, one creates an object with all its needed dependencies, and then uses the object. However in some cases, the information needed to create a dependency is not known before the object is used.</p>
  <p>I will try to create a concrete example that has to do with making a house. The following will be the dependencies</p>
  <pre><code>class WallMakerAlgorithm(abc.ABC):
      def build(self):
          # Put specific algorithm here that will build the wall
          pass
  
  class DoorMakerAlgorithm(abc.ABC):
      def __init__(self, color: float, material: str):
          # subclasses can override init to add more fancy stuff
          self.color = color
          self.material = material
  
      def build(self):
          # Put specific algorithm here that will build the door
          pass
  
  </code></pre>
  <p>These are used by a house builder class. In the ideal case, a <code>HouseBuilder</code> will know its dependencies on init:</p>
  <pre><code>class BasicHouseBuilder:
      def __init__(self, door_maker_algorithm: DoorMakerAlgorithm, wall_maker_algorithm: WallMakerAlgorithm):
          self.door_maker_algorithm = door_maker_algorithm
          self.wall_maker_algorithm = wall_maker_algorithm
  
      def build(self):
          self.wall_maker_algorithm.build()
          self.door_maker_algorithm.build()
  </code></pre>
  <p>But now assume that for some (contrived) reason, the customer hasn't decided on the color of the doors. They tell you that once the wall is built, they will know the color because they will be able to see it. This creates a chicken and egg kind of problem. I can't create the <code>HouseBuilder</code> because I can't create the door making algorithm, and I can't create the door making algorithm without actually running <code>HouseBuilder</code>. Something like this:</p>
  <pre><code>class DecideColorLaterHouseBuilder:
     def __self__(self, wall_maker_algorithm: WallMakerAlgorithm, query_color: Callbale, ???):
        self.wall_maker_algorithm = wall_maker_algorithm
        self.query_color = query_color
        ???
  
     def build(self):
        self.wall_maker_algorithm.build()
        desired_door_color = self.query_color()
        # Now I want to build the door. But I don't have the door algorithm because I don't know the color!
  </code></pre>
  <p>Is there some design pattern that deals with this kind of problem? Are my abstractions just messed up in general? I can potentially add <code>color</code> as an argument to <code>build</code> of the <code>DoorMakingAlgorithm</code> but that just messes up my abstractions.</p>