7

Is there any significant difference between the two Python keywords ... and pass I should be aware of? For example:

def tempFunction(): pass 
def tempFunction(): ... 
5
  • 3
    ... is not Python syntax, last I checked. Perhaps you've copied this out of some examples? If so please share a reference Commented Sep 28, 2022 at 6:16
  • 13
    @OneCricketeer That is the Elipsis, it's perfectly valid python syntax. It's a global constant just like None. Commented Sep 28, 2022 at 6:19
  • Does this answer your question? What does the Ellipsis object do? Commented Sep 28, 2022 at 6:20
  • 2
    One difference is that pass has been around since the beginning, while ... has only relatively recently been allowed in this kind of context. Both do exactly the same thing (which is absolutely nothing), but they have rather different idiomatic meanings: pass is for places where you don't ever intend to do anything, but have to write something for syntactic reasons (your empty def is a good example of that); ... is for places where you intend to fill in the blank later. Commented Sep 28, 2022 at 6:22
  • @jasonharper Many people prefer using ... to indicate empty blocks and some coding styles require it, it's a matter of opinion. Commented Sep 28, 2022 at 6:26

4 Answers 4

8

The ... is the shorthand for the Ellipsis global object in python. Similar to None and NotImplemented it can be used as a marker value to indicate the absence of something.

For example:

print(...) # Prints "Ellipsis" 

In this case, it has no effect. You could put any constant there and it would do the same. This is valid:

def function(): 1 

Or

def function(): 'this function does nothing' 

Note both do nothing and return None. Since there is no return keyword the value won't be returned.

pass explicitly does nothing, so it will have the same effect in this case too.

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

Comments

4

The ... is an ellipsis, aka internal object Ellipsis.

def tempFunction(): ... 

is the same as:

def tempFunction(): Ellipsis 

so it's similar to doing something like:

def tempFunction(): 0 

All of these are functions which have a simple expression in that doesn't get returned so almost the same as using pass. Not exactly the same as the expression still gets evaluated even though the value is never used.

I just use pass. It's the most efficient and understood by every Python programmer.

Comments

1

pass is a keyword. It's basically a null operator and when it is executed, "nothing happens".

... is an object of type <class 'ellipsis'>. When it appears, it's an expression like any other objects it may also be evaluated.

But whether this Ellipsis object is evaluated (in your example) or not varies between different versions of the interpreter.

In my local machine(3.10.6):

from dis import dis def fn_ellipsis(): ... def fn_pass(): pass dis(fn_ellipsis) print("-----------------------------------") dis(fn_pass) 

output:

 4 0 LOAD_CONST 0 (None) 2 RETURN_VALUE ----------------------------------- 7 0 LOAD_CONST 0 (None) 2 RETURN_VALUE 

but in 3.5.1:

 4 0 LOAD_CONST 1 (Ellipsis) 3 POP_TOP 4 LOAD_CONST 0 (None) 7 RETURN_VALUE ----------------------------------- 7 0 LOAD_CONST 0 (None) 3 RETURN_VALUE 

So it's better to say, it's guaranteed that pass won't evaluated at all but evaluation of any other object is implementation details. New parser can easily say that it's unnecessary to evaluate a bare ... in your example. Same thing happens if you place an int. But that's not the case for list objects.

Comments

-1

So Apparently pass can't be used with If statements but ... can

for example, here, the python interpreter will not accept the if pass: statement

 while True: if pass : continue else: break 

i get syntax error

 if pass : ^^^^ SyntaxError: invalid syntax Process finished with exit code 1 

but using ... it works.

 while True: if ... : continue else: break 

which leads me to believe that ... has more use cases where pass would fail

I don't know why and I don't know if there are more scenarios like this but i bet there is, and if anyone can explain why this happens and if there's more scenarios like it that would be great.

8 Comments

Why were you trying if pass in the first place? That doesn't make any sense as far as I can tell.
to create an empty if statement that does nothing, like a placeholder , like i did with if ... :
Right, but why pass? It seems like you just misunderstood how pass works, but I'm not sure if there's more to it than that.
Yes Exactly, i didn't understand the difference that's why i came to this thread to ask about this.
An if condition can't be "empty". It must evaluate to either true or false, it can't just be neither. If you want a placeholder condition that makes the if statement not execute, do if False; if you want one that does make the condition execute, do if True. If you have a condition, but don't know yet what to do afterwards, use pass as a placeholder statement for the block following the condition: if foo > bar: pass.
I did try if True but it didn't produce the result that i wanted because it made the Break statement unreachable because it's always True from While True: statement, but i see what you mean now thanks, pass won't work because it can't be evaluated to True or False
pass is a statement. Statements are standalone and cannot be used inside other statements. For the same reason, if while or if def fail too. ... is an expression, which can be used anywhere an expression is accepted. Basic language grammar.
I see, thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.