Anton Barycheuski
Anton Barycheuski

Reputation: 720

Non-ASCII identifiers for python 2.7

I know that in python 3.x I can use Non-ASCII identifiers (PEP 3131).

x1 = 2
x2 = 4
Δx = x2 - x1
print(Δx)

Is there such feature in python 2.7? Maybe, has anybody ported it to 2.x branch?

Upvotes: 3

Views: 261

Answers (2)

Eevee
Eevee

Reputation: 48556

Well, I mean, technically this is what you asked for:

>>> x1 = 2
>>> x2 = 4
>>> locals()[u'Δx'] = x2 - x1
>>> print locals()[u'Δx']
2

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1123420

No, there is no such feature in Python 2; names are constricted to using ASCII letters and digits only.

See the Identifiers and Keywords section of the reference manual:

Identifiers (also referred to as names) are described by the following lexical definitions:

identifier ::=  (letter|"_") (letter | digit | "_")*
letter     ::=  lowercase | uppercase
lowercase  ::=  "a"..."z"
uppercase  ::=  "A"..."Z"
digit      ::=  "0"..."9"

It was PEP 3131 that expanded the range of possible characters for Python 3.

There is little point in porting this to the 2.x branch; it would remain a niche 'feature' that requires everyone running your code to install a specially patched and compiled interpreter.

Note that the change is not trivial; Python 2 identifiers are byte strings, not unicode values. You'd have to find all locations in the interpreter that handle identifiers and verify that these can handle non-ASCII values or retool these for unicode strings instead. This goes way beyond the compiler!

Upvotes: 7

Related Questions