Is there any significant difference between the two Python keywords ... and pass I should be aware of? For example:
def tempFunction(): pass def tempFunction(): ... 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.
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.
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.
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.
if pass in the first place? That doesn't make any sense as far as I can tell.if ... :pass? It seems like you just misunderstood how pass works, but I'm not sure if there's more to it than that.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.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 Falsepass 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.
...is not Python syntax, last I checked. Perhaps you've copied this out of some examples? If so please share a referencepasshas 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:passis for places where you don't ever intend to do anything, but have to write something for syntactic reasons (your emptydefis a good example of that);...is for places where you intend to fill in the blank later....to indicate empty blocks and some coding styles require it, it's a matter of opinion.