8

So I'm a terrible person and I want to name a variable in my mathy-python3 code s′ (that's U+2032 PRIME).

I was under the impression Unicode literals work as identifiers in Python 3, which is why my ɣ, α, and ε's are working fine.

Is the prime symbol specifically forbidden because of how terrible it would be to use?

Philip Kendall
26.1k10 gold badges66 silver badges68 bronze badges
asked Oct 7, 2014 at 15:48
0

4 Answers 4

14

You cannot just use any Unicode character in Python identifiers. There are rules governing what can be used:

identifier ::= xid_start xid_continue*
id_start ::= <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
id_continue ::= <all characters in id_start, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>
xid_start ::= <all characters in id_start whose NFKC normalization is in "id_start xid_continue*">
xid_continue ::= <all characters in id_continue whose NFKC normalization is in "id_continue*">

The Unicode category codes mentioned above stand for:

Lu - uppercase letters
Ll - lowercase letters
Lt - titlecase letters
Lm - modifier letters
Lo - other letters
Nl - letter numbers
Mn - nonspacing marks
Mc - spacing combining marks
Nd - decimal numbers
Pc - connector punctuations
Other_ID_Start - explicit list of characters in PropList.txt to support backwards compatibility
Other_ID_Continue - likewise

The above is the Unicode equivalent to any number of letters, digits and underscores, as long as you don't start with a digit.

The U+2032 PRIME codepoint doesn't fall in any of those classes; it is considered Po - other punctuation instead.

I've linked the Unicode categories to codepoint.net queries for you so you can see what kind of characters are permitted. Your other identifier choices are all Ll lowercase letter characters, by the way.

You do want to be careful and use some common sense in what you use as identifiers. Keep them readable and recognisable. Preferably they should be easy to type too, so personally I'd steer away from using greek lowercase letters myself. I'd rather use alpha, gamma, epsilon and s_prime instead here.

answered Oct 7, 2014 at 15:53
3

You can use U+02BC MODIFIER LETTER APOSTROPHE: ʼ.

aʼ = "ok"
print(aʼ)

... prints "ok".

Should you use it in Python 3? As others have said, certainly not. But coming from an OCaml background, I must confess U+0027 APOSTROPHE (') in identifiers is quite useful, especially for mathy-code.

answered Dec 4, 2021 at 11:43
2

It looks like a single-quote character, which is probably what most programmers would simply assume, unless you told them, or they squinted and looked really close. Heck, there could be fonts that use the same glyph for both characters. Even if you can use it, I'd avoid it, to avoid the possible confusion it might cause.

answered Oct 7, 2014 at 15:53
0

You can use "Modifier letter prime" (U+02B9) and "Modifier letter double prime" (U+02BA) as part of a Python identifier.

Mapping AltGr+' and AltGr+" to prime and double prime make them easily available.

Some monospaced fonts do a good job in distinguishing them from single and double quotes, others are not so good (see screenshot – prime on the left, quotes on the right).

prime (left) and quotes (right) symbols rendered with some monospaced fonts

answered Feb 16, 2023 at 10:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.