The Python 🐍 problem-solving bootcamp 🚀 is starting soon. Join the second cohort now!

Today I learned you can have invisible variables in Python.

The Python 🐍 problem-solving bootcamp is starting soon. Join the second cohort now!

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
>>> ï¾ 
17

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!

Take your Python 🐍 skills to the next level 🚀

I write about Python every week. Join +16.000 others who are taking their Python 🐍 skills to the next level 🚀, one email at a time.

References

Previous Post Next Post

Blog Comments powered by Disqus.