avatarYang Zhou

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2131

Abstract

t get stuck in details right away.</p><p id="3e8b">By the way, the “pass” keyword can also be used as a placeholder in Python:</p><div id="aa2c"><pre>def write_an_article(): pass

<span class="hljs-keyword">class</span> <span class="hljs-symbol">Article: <span class="hljs-symbol">pass</span></span></pre></div><p id="2446">We can decide which one to use based on personal preference. As far as I am concerned, the ellipsis is cuter.</p><h1 id="9043">2. Use an Ellipsis in Numpy To Omit Dimensions</h1><p id="6baa">The Numpy is an essential Python library for Data Science. When handling multi-dimension arrays in Numpy, the ellipsis is helpful.</p><p id="c3aa">For example, if we have a 3-d matrix and want to slice it, there are three ways:</p> <figure id="77ac"> <div> <div>

            <iframe class="gist-iframe" src="/gist/ZhouYang1993/ecdce98488689378d5d49d0bfdaefaa7.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="6a5b">As shown above, using the three dots is the most economical way to slice a multi-dimension matrix. Because it requires the least typing stuff. Programmers’ time is money, isn’t it?</p><h1 id="40e8">3. Use the Ellipsis for Type Hinting</h1><p id="559b">The type hinting was a new feature from Python version 3.5. Base on the <a href="https://www.python.org/dev/peps/pep-0484/">PEP 484</a>, the ellipsis has special usages within this feature.</p><p id="6c1c">On the one hand,</p><blockquote id="edd7"><p><i>Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example, <code>Tuple[int, ...]</code>.</i></p></blockquote><p id="6332">On the other hand,</p><blockquote id="6a5e"><p><i>It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis (three dots) for the list of arguments:</i></p></blockquote><div id="9e4c"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">partial</span>(<spa

Options

n class="hljs-params">func: <span class="hljs-type">Callable</span>[..., <span class="hljs-built_in">str</span>], *args</span>) -> <span class="hljs-type">Callable</span>[..., <span class="hljs-built_in">str</span>]: <span class="hljs-comment"># Body</span></pre></div><h1 id="ecc7">Conclusion</h1><p id="577e">The ellipsis is an interesting syntax sugar in Python. It’s helpful in some scenarios, and more importantly, it’s cute! 😄</p><p id="af00">By the way, the reason why Guido van Rossum added this syntax into Python is because he agreed that <a href="https://mail.python.org/pipermail/python-3000/2008-January/011793.html">it’s cute</a>.</p><p id="65c0"><b><i>Thanks for reading. If you like it, don’t forget to follow <a href="https://yangzhou1993.medium.com/follow">me</a> to get more great articles about programming and technologies!</i></b></p><p id="7bd0"><b><i>More interesting features in Python:</i></b></p><div id="efdb" class="link-block"> <a href="https://readmedium.com/5-uses-of-asterisks-in-python-3007911c198f"> <div> <div> <h2>5 Uses of Asterisks in Python</h2> <div><h3>The powerful weapon for writing more elegant code</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*35U6I_L0COYwYQdzUYrpQw.jpeg)"></div> </div> </div> </a> </div><div id="c094" class="link-block"> <a href="https://readmedium.com/7-levels-of-using-the-zip-function-in-python-a4bd22ee8bcd"> <div> <div> <h2>7 Levels of Using the Zip Function in Python</h2> <div><h3>Do more by less code</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*zvzb_5O8DfGCGT-N3RvAow.jpeg)"></div> </div> </div> </a> </div></article></body>

3 Uses of the Ellipsis in Python

The cutest syntax sugar in Python

Photo by Mike Szczepanski on Unsplash

Python has a special built-in singleton object called “Ellipsis”. If we input three dots or the word “Ellipsis” in a Python interactive shell, the results are as following:

>>> ...
Ellipsis
>>> Ellipsis
Ellipsis

This simple object seems inconspicuous, but if we use it properly, it can make our lives easier.

This article will introduce the common three scenarios where the ellipsis can be used. After reading, you’ll like this cute singleton object of Python. 😃

1. An Ellipsis Is a Placeholder for Unwritten Code

When designing a new module, we usually define some functions or classes but won’t implement them immediately. Because we only want to determine what we need to write in future and don’t care too much about the implementation details at this early stage. In this scenario, the ellipsis is our best friend:

def write_an_article():
    ...


class Article:
    ...

As shown above, we can just use an ellipsis as a placeholder for a function or a class.

Generally speaking, it’s a good programming practice that we design the needed things at first and implement them later. Because this way can help our mind keep clear about the whole structure and don’t get stuck in details right away.

By the way, the “pass” keyword can also be used as a placeholder in Python:

def write_an_article():
    pass


class Article:
    pass

We can decide which one to use based on personal preference. As far as I am concerned, the ellipsis is cuter.

2. Use an Ellipsis in Numpy To Omit Dimensions

The Numpy is an essential Python library for Data Science. When handling multi-dimension arrays in Numpy, the ellipsis is helpful.

For example, if we have a 3-d matrix and want to slice it, there are three ways:

As shown above, using the three dots is the most economical way to slice a multi-dimension matrix. Because it requires the least typing stuff. Programmers’ time is money, isn’t it?

3. Use the Ellipsis for Type Hinting

The type hinting was a new feature from Python version 3.5. Base on the PEP 484, the ellipsis has special usages within this feature.

On the one hand,

Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example, Tuple[int, ...].

On the other hand,

It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis (three dots) for the list of arguments:

def partial(func: Callable[..., str], *args) -> Callable[..., str]:
    # Body

Conclusion

The ellipsis is an interesting syntax sugar in Python. It’s helpful in some scenarios, and more importantly, it’s cute! 😄

By the way, the reason why Guido van Rossum added this syntax into Python is because he agreed that it’s cute.

Thanks for reading. If you like it, don’t forget to follow me to get more great articles about programming and technologies!

More interesting features in Python:

Programming
Python
Data Science
Technology
Coding
Recommended from ReadMedium