Today I learned you can have invisible variables in Python.

What characters can you use in Python variable names?

The exact definition of what characters are valid can be found in the Python docs, but a broad definition could be "all characters that can be used in written languages".

Here are some examples of Python variables that use non-ASCII characters:

빵 = "bread"  # Korean
ψωμί = "bread"  # Greek
面包 = "bread"  # Chinese (Simplified)
ขนมปัง = "bread"  # Thai
pão = "bread"  # Portuguese
талх = "bread"  # Mongolian
パン = "bread"  # Japanese

(I used Google translate for this... Please let me know if there is a mistake here!)

The exact list of all valid characters is gigantic. But, in the midst of that gigantic list, four characters stand out:

These four characters are invisible but I do not think they are considered whitespace. At least, not to the Unicode standard. And, thus, they can be used in Python variables!

First, let me show you the literal transcript of a REPL session in which I use those four characters to represent four variables:

>>> def ᅟ(a, b):   # Function name is U+115F
...     return a + b + 1
>>> ᅠ = 6          # Variable name is U+1160
>>> ㅤ = 8         # Variable name is U+3164
>>> ᅠ = ᅟ(ᅠ, ㅤ)  # Result is U+FFA0
>>> ᅠ

Depending on your system, you may be able to see something for the first two characters, but the last two should be invisible! So, you can have invisible variable names in Python.

That's it for now! Stay tuned and I'll see you around!

Become a better Python 🐍 developer 🚀

+35 chapters. +400 pages. Hundreds of examples. Over 30,000 readers!

My book “Pydon'ts” teaches you how to write elegant, expressive, and Pythonic code, to help you become a better developer. >>> Download it here 🐍🚀.


Previous Post Next Post

Blog Comments powered by Disqus.