Non-ASCII identifiers for python 2.7

前端 未结 2 1777
失恋的感觉
失恋的感觉 2021-01-20 16:45

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条回答
  • 2021-01-20 17:04

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

    >>> x1 = 2
    >>> x2 = 4
    >>> locals()[u'Δx'] = x2 - x1
    >>> print locals()[u'Δx']
    2
    
    0 讨论(0)
  • 2021-01-20 17:24

    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!

    0 讨论(0)
提交回复
热议问题