Search code examples
pythonstubstubbing

Stubbing out functions or classes


Can you explain the concept stubbing out functions or classes taken from this article?

class Loaf:
    pass  

This class doesn't define any methods or attributes, but syntactically, there needs to be something in the definition, so you use pass. This is a Python reserved word that just means “move along, nothing to see here”. It's a statement that does nothing, and it's a good placeholder when you're stubbing out functions or classes.`

thank you


Solution

  • 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.