74

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

3
  • 4
    In a half-convention, I often see ... used where people want to indicate something they intend to fill in later (a 'todo' empty block) and pass to mean an block intended to have no code >>> A comment from stackoverflow.com/a/6189281/11890300 Commented May 26, 2020 at 10:28
  • This is not equivalent pieces of code. ... is a literal: type(...) <class 'ellipsis'>. It is just like doing def f(): 5. So to your question: pass Commented May 26, 2020 at 10:29
  • 1
    I'm not sure why this was closed as a duplicate when the linked question only covers one of the things being asked about (Ellipsis)... I'm not sure I want to stir the pot here and vote to reopen, but I at least added another link to cover the other thing being asked about (pass). Commented Oct 1 at 12:32

2 Answers 2

66

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

Sign up to request clarification or add additional context in comments.

1 Comment

One important thing to note is that - pass doesn't go well with typing and code editor will flag error when a function is supposed to return something but it just has a pass statement. On the other hand, same function with ... will not show any typing error.
19

Not exactly an answer to your question, but perhaps a useful clarification. The pass statement should be use to indicate a block is doing nothing (a no-op). The ... (ellipsis) operator is actually a literal that can be used in different contexts.

An example of ellipsis usage would be with NumPy array indexing: a[..., 0]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.