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)
I am shocked to see that Python can be made uglier than it already is!
Unbelievable! I think Python just stole the title from perl for being able to produce unintelligible code.
It was easy to comprehend ;)
By the way, why it is not allowed to use f(1, …, 23)?
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.
Sorry, @zhvernq — not sure what happened to your comments. I tried to fix the formatting and WordPress deleted your code…
It is a miricle I have no idea how it works wheres the comments that you say you completed?
I could have sworn you said something about explaining….