Stubbing out functions or classes

stubbing out functions or classes

This refers to writing classes or functions but not yet implementing them. For example, maybe I create a class:

class Foo(object):
     def bar(self):
         pass

     def tank(self):
         pass

I've stubbed out the functions because I haven't yet implemented them. However, I don't think this is a great plan. Instead, you should do:

class Foo(object):
     def bar(self):
         raise NotImplementedError

     def tank(self):
         raise NotImplementedError

That way if you accidentally call the method before it is implemented, you'll get an error then nothing happening.


A 'stub' is a placeholder class or function that doesn't do anything yet, but needs to be there so that the class or function in question is defined. The idea is that you can already use certain aspects of it (such as put it in a collection or pass it as a callback), even though you haven't written the implementation yet.

Stubbing is a useful technique in a number of scenarios, including:

  • Team development: Often, the lead programmer will provide class skeletons filled with method stubs and a comment describing what the method should do, leaving the actual implementation to other team members.
  • Iterative development: Stubbing allows for starting out with partial implementations; the code won't be complete yet, but it still compiles. Details are filled in over the course of later iterations.
  • Demonstrational purposes: If the content of a method or class isn't interesting for the purpose of the demonstration, it is often left out, leaving only stubs.