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)?
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.
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()
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With