← Back to Recent PostsI Hate Ellipses in Python

I Hate Ellipses in Python

January 16, 2023

Or why you should just be explicit.

... What?

Python has a pass keyword. This is useful in a whole bunch of situations. I mostly use it when stubbing out my functions:

def do_something():
  pass

def do_something_else():
  pass

if __name__ == '__main__':
  do_something()
  do_something_else()

This let's me stub out the function signature while ignoring the actual logic while I work on other things.

Python also has an "ellipses literal": .... In practice, it's used much the same way.

def do_something():
  ...

def do_something_else():
  ...

if __name__ == '__main__':
  do_something()
  do_something_else()

This code will behave exactly the same. Each function will be skipped without raising the SyntaxError which would be raised if you just left the function blank. It's not limited to functions, either; both the pass keyword and the ellipses literal can be used in any code block:

@app.route('/some/path')
def handle_(req, res):
  if (req.method === 'GET'):
    pass
  elif (req.method === 'POST'):
    ...

Okay, so what?

It's terrible, that's what. I hate it. I hate the ellipses literal with a passion.

1. It's Not Explicit

The first time I saw an ellipses literal used I had absolutely no idea what it was and why it was there. I think I saw it in a blog post first, so I just assumed the author left out the code for brevity's sake.

Then I saw it in real-life code and I still had no idea what it was and why it was there. I mean, I guess I could figure it out, kind of, but not really.

def do_something():
  ...

I mean, what is that doing? Is my blog post just cutting off the contents of the do_something function? Is my editor? Who knows.

No matter what, pass is explicit in what it's doing. Even if you have never seen it before you get what it's doing right off. If you have never worked with Python which do you think would get you up-to-speed in a codebase faster?

2. It's Impossible to Google

Have you ever tried to google three periods, ...? It doesn't work. Try ... python and you just get the Python homepage. The only way to get info about it is to google "ellipses python." I think we all have better things to do than to type out "ellipses." (Okay, I might be overreacting but only a little bit.)

Compare this to googling pass python and you are rewarded with several resources for using the pass keyword in Python.

3. Find And Replace Sucks

Okay, so it might not really be any easier to find and replace pass vs ..., but I do think it's easier to read and act on the find results for pass than ....

Caveat

I know someone out there is just waiting to "well actually" me about this. I'll do it for you.

Well actually, if you want to be explicit you should raise a NotImplementedError

You're right! And if I'm writing out a stub that I know will be committed and worked on by others I will do just that.

def do_something():
  raise NotImplementedError('This should do something')

This is also helpful when testing since this specific error will be raised. Leaving pass or ... can result in some insidious errors that are hard to track down.

Well actually, ... reads like the should be code there while pass reads like you're passing on that code block.

I mean... I guess? Again, I don't really ever want to commit pass or .... I'm mostly using it when I'm stubbing out functions or if/else blocks.

Well actually, this is just total personal preference.

Yep.

(I'm right, though)

Conclusion

I hate the ellipses literal. It's not explicit, it's hard to learn about, and it's just ugly. Please use pass instead (or raise a NotImplementedError).