12

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

xralf
  • 3,312
  • 45
  • 129
  • 200

6 Answers6

28

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
  • 1
    So, stubbing is only a reminder that I need to implement something? – xralf Oct 01 '11 at 13:54
  • 1
    @xralf Not always. Sometimes you want a no-op function and the only way to declare an empty block is by using pass (kind of like empty curly braces in a language such as ``C++``). – awesomo Oct 01 '11 at 15:31
  • @awesomo, would you call that a stub though? I think of a stub as being incomplete whereas a deliberate no-op is not incomplete. – Winston Ewert Oct 01 '11 at 15:42
  • @WinstonEwert I agree. My comment was more to address the OPs apparent lack of understanding of the ``pass`` keyword (or so it seemed from the question). – awesomo Oct 01 '11 at 17:24
  • @awesomo, as I read the question it was just about what stubbing meant which came across in the context of pass. I don't see evidence that's not understanding pass. – Winston Ewert Oct 01 '11 at 18:00
  • Yes, it was just about stubbing, other things are described in the article. – xralf Oct 01 '11 at 18:07
  • In testing, stubs are usually code that returns a dummy value rather than doing any work. That could be to allow you to work on the code's dependencies without implementing the code, or as a sort of mock object to test the dependent code even when your real code has been implemented. – Simon Hibbs Mar 15 '17 at 14:18
  • @SimonHibbs, true but not releveant to what the question was asking. – Winston Ewert Mar 15 '17 at 23:56
8

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.
tdammers
  • 20,353
  • 1
  • 39
  • 56
7

Note that you can stub functions like this:

def get_name(self) -> str : ...
def get_age(self) -> int : ...

(yes, this is valid python code !)

It can be useful to stub functions that are added dynamically to an object by a third party library and you want have typing hints. Happens to me... once :-)

Dominique PERETTI
  • 1,063
  • 12
  • 12
6

Ellipsis ... is preferable to pass for stubbing.

pass means "do nothing", whereas ... means "something should go here" - it's a placeholder for future code. The effect is the same but the meaning is different.

bcb
  • 1,977
  • 2
  • 22
  • 21
1

Stubbing is a technique in software development. After you have planned a module or class, for example by drawing it's UML diagram, you begin implementing it.

As you may have to implement a lot of methods and classes, you begin with stubs. This simply means that you only write the definition of a function down and leave the actual code for later. The advantage is that you won't forget methods and you can continue to think about your design while seeing it in code.

Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
0

The reason for pass is that Python is indentation dependent and expects one or more indented statement after a colon (such as after class or function).

When you have no statements (as in the case of a stubbed out function or class), there still needs to be at least one indented statement, so you can use the special pass statement as a placeholder. You could just as easily put something with no effect like:

class Loaf:
    True

and that is also fine (but less clear than using pass in my opinion).

awesomo
  • 8,526
  • 2
  • 21
  • 24