Prime symbol in Python variable name

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?

0

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.

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.

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.

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật