Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between pass statement and 3 dots(...) in python [duplicate]

Tags:

python

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

like image 527
Igor Alex Avatar asked May 26 '20 10:05

Igor Alex


People also ask

What do 3 dots mean in Python?

In Short: Use the Ellipsis as a Placeholder in Python That means you can use an ellipsis as a placeholder similar to the pass keyword. Using three dots creates minimal visual clutter. So, it can be convenient to replace irrelevant code when you're sharing parts of your code online.

What do the dots mean in Python?

Almost everything in Python is an object. Every object has certain attributes and methods. The connection between the attributes or the methods with the object is indicated by a “dot” (”.”) written between them. For example if dog is a class, then a dog named Fido would be its instance/object. class Dog: Fido = Dog()

What are pass statements in Python?

Python pass Statement The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed. Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

What is triple dots in Numpy?

The ellipsis (three dots) indicates "as many ':' as needed". (Its name for use in index-fiddling code is Ellipsis, and it's not numpy-specific.) This makes it easy to manipulate only one dimension of an array, letting numpy do array-wise operations over the "unwanted" dimensions.


1 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

like image 183
Felk Avatar answered Sep 29 '22 23:09

Felk