Reputation: 30717
I have been trying to figure out how to use non-ascii unicode characters as Python 3 variables but I'm not sure which ones work and which ones don't. Why does the σ
work but ∆
does not? Is it possible to use ∆
as a character or is it impossible at the moment?
# Version
sys.version
'3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33) \n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]'
σ = 10
print(σ )
# 10
∆ = 20
# File "<ipython-input-24-b5e030117292>", line 1
# ∆ = 20
# ^
# SyntaxError: invalid character in identifier
Upvotes: 2
Views: 1953
Reputation: 365925
This is documented under Identifiers in the Python reference.
But the short version is that you can only use letters and numbers and a few special connectors in identifiers, and ∆
(U+2206
, INCREMENT
) is not a letter, it's a mathematical operator (part of the Symbol, Math
category).
If you were intending to use Δ
(U+0396
, GREEK CAPITAL LETTER DELTA
)… well, that's an understandable mistake, since they not only look very close or identical in most fonts, but are usually not distinguished even in blackboard writing (the whole point of the "increment" symbol is that it's a delta). But they're not the same character, and only the one that's meant to be used as a letter can be part of an variable name.
Also, the docs mention but don't link to the Unicode standard that Python's standard is based on, so here's the link: UAX-31: Unicode Identifier and Pattern Syntax (aka TR-31).
Upvotes: 7