Answer a question

What's the difference between the pass statement:

def function():
    pass

and 3 dots:

def function():
   ...

Which way is better and faster to execute(CPython)?

Answers

pass has been in the language for a very long time and is just a no-op. It is designed to explicitly do nothing.

... is a token having the singleton value Ellipsis, similar to how None is a singleton value. Putting ... as your method body has the same effect as for example:

def foo():
    1

The ... can be interpreted as a sentinel value where it makes sense from an API-design standpoint, e.g. if you overwrite __getitem__ to do something special if Ellipsis are passed, and then giving foo[...] special meaning. It is not specifically meant as a replacement for no-op stubs, though I have seen it being used that way and it doesn't hurt either

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐