Emojis as Python Variables? Sure, Why Not!
An oddly satisfying way to write python code by leveraging the pythonji library
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.
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
2. Convert images into 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:
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:
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!