开发者

Stubbing out functions or classes

开发者 https://www.devze.com 2023-04-10 13:39 出处:网络
Can you explain the concept stubbing out functions or classes taken from this article? class Loaf: pass

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

class Loaf:
    pass  

This class doesn't defin开发者_C百科e 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


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.


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 :-)


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消