Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 26, 2020 08:57 pm GMT

The Art of doing nothing: Python Edition

Writing programs can be stressful. As a developer, I have never been one to always pen down pseudo-codes before writing my first lines of code so it happens that at some point I do not know what to write. Also, when writing modular scripts, I define all classes/functions and their doc-strings at the start before completing them one after the other.

This means I had to find ways to make empty classes or functions not raise errors when the script is run. I have discovered a couple of ways to do this and I will be sharing them in this article. Whether you are like me or not, I do not doubt that you will find this interesting and useful.

The package called nothing

The mere fact that a python developer created this is quite fascinating. Here we have a package that contains nothing but a version number and a one-sentence description in its init file. In fact, this is the only description for this package on Github and PyPI, nothing else. Like every other package, it can be installed with pip via the command pip install nothing.

Once it is installed, we can import nothing and then use it as a placeholder as shown below

The pass statement

This is the more popular option. pass is an inbuilt null statement in python that is used to stand in for code that is yet to be written. Its inbuilt nature makes it most python developers goto statement for code blocks that are to do nothing. It is used like the nothing package as we'll see in the image below.

Alt Text

Ellipsis (...)

In the English language, an ellipsis is used when omitting a word, phrase, line, paragraph, or more from a quoted passage. In Python, however, an ellipsis is a single object with no methods that serve a range of functions from multi-dimensional array slicing to type hinting but we will be focusing on its use as a placeholder. This is a personal favourite because I find the similarity between its use case in python and regular English texts interesting. We see it used in the image below as a part of a recursive function with only the base case specified.

Alt Text

The final straw

This last case is quite intriguing. Its something you would have thought of at some point and simply put, it is leaving your editor empty. Why type when you are not ready yet? Finish up that pseudo-code then open your editor and code away. Yes, this is a message to me too

Alt Text

I hope I successfully communicated the art of doing nothing in Python to you all and that this was enjoyable.


Original Link: https://dev.to/enutrof/the-art-of-doing-nothing-python-edition-6e3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To