Python’s Ellipsis explained

As others note, using Python’s Ellipsis object in slices is rather obscure, and there are hardly any good examples out there. So I thought I’d do my bit with a nice, simple example, complete with comments:

# Portable way to get the You-Know-Which object without naming it
class __:
 def __getitem__(__, _):
  return _
___ = __()[...]

# An Ellipsobinary-to-ASCII convertor
class __:
 def __getitem__(__, _):
  return chr(sum(1<<i if _[-i-1] is ___ else 0 for i in range(len(_))))
_ = __()

# Finally, use the That-Which-Must-Not-Be-Named object
print (
 _[...,_,_,...,_,_,_] +
  _[...,...,_,_,...,_,...] +
   _[...,...,_,...,...,_,_] +
    _[...,...,_,...,...,_,_] +
     _[...,...,_,...,...,...,...] +
      _[...,_,...,...,_,_] +
       _[...,_,_,_,_,_] +
        _[...,...,...,_,...,...,...] +
         _[...,...,_,...,...,...,...] +
          _[...,...,...,_,_,...,_] +
           _[...,...,_,...,...,_,_] +
            _[...,...,_,_,...,_,_] +
             _[...,_,_,_,_,...])

And you thought Python code couldn’t be obfuscated?

Seriously, though … do any non-NumPy programmers actually use Ellipsis? I have yet to find other (non-NumPy) uses of it in the wild.

8 May 2009 by Ben    7 comments

7 comments (oldest first)

Nuno Furtado 8 May 2009, 19:23 link

I could have sworn you said something about explaining….

dude 9 May 2009, 00:20 link

I am shocked to see that Python can be made uglier than it already is!

Brian Wojcik 9 May 2009, 02:33 link

Unbelievable! I think Python just stole the title from perl for being able to produce unintelligible code.

Szabolcs 13 Jan 2013, 01:05 link

It was easy to comprehend ;)

By the way, why it is not allowed to use f(1, …, 23)?

Steve Holden 7 Jun 2014, 08:42 link

Just a quick note to point out that, like None, there is only one instance of the Ellipsis object in a Python interpreter. The easiest way to get hold of the object is to use its name. So you could write

x = Ellipsis

but you could just as easily use the name Ellipsis in any context where the … is syntactically unacceptable.

Ben 10 Jul 2015, 09:28 link

Sorry, @zhvernq — not sure what happened to your comments. I tried to fix the formatting and WordPress deleted your code…

pebaudhi 15 May 2018, 16:06 link

It is a miricle I have no idea how it works wheres the comments that you say you completed?