Better Programming

Advice for programmers.

Follow publication

Emojis as Python Variables? Sure, Why Not!

Pratik Choudhari
Better Programming
Published in
2 min readNov 15, 2021

--

Photo by wu yi on Unsplash

Emojis can express a lot, so why not use them to write code, sounds ridiculous, right? There are many articles that promote the use of emojis in git commit messages and comments to improve overall communication efficacy.

Python is not made to handle emojis as variable names — not a feature developers would give priority to. However, a library called pythonji enables a user to do so. Users can not only use an emoji as a variable name but it can also be used as an alias in the import statement.

In this article, you’ll learn how to write code using emoji and execute it. Along with this, you’ll also see how the code is translated to be understood by the python interpreter.

Installation

Pythonji works on 3.6 ≤ Python ≤ 3.8 only.

python -m pip install pythonji

Writing the Emoji-code

To write code that includes emojis is fairly simple, take a look at the following example.

pythonji turtle code to create a square

Save this file as square.🐍 Yes, you have to use a python emoji as an extension to be able to use pythonji. Here, I have used .py extension for syntax highlighting. This file can not be understood by a Python interpreter directly, pythonji acts as middleware and substitutes emojis in the code for variable names.

Execute the file using pythonji:

pythonji square.🐍

Examples

1. Using Pillow to read and display an image

pythonji PIL code to open a image file

2. Convert images into JPG

pythonji code to convert images in jpg

Making sense of the Emoji-code

As we know python can not understand any of the code I have shown above, so pythonji has to do the translation by replacing all emoji occurrences with variable names. Let’s take this snippet as a sample:

Source: Pythonji Examples

By making a few changes in the pythonji source code we can see how the code above is converted to be understood by python interpreter:

pythonji’s translated version

Conclusion

This is an unconventional way to write code and I believe no one has this in their production environments.

I don’t know if pythonji maintainers are going to take this as a serious project or if this was just for fun, but we can learn from their source code, which is clean and comprehensible even for a newbie.

Thanks for reading. Until next time!

--

--

Pratik Choudhari
Pratik Choudhari

Written by Pratik Choudhari

Blending engineering and analytics. Join me as I share my experiences!

Responses (2)

Write a response