Unicode identifiers in Python?

后端 未结 5 609
[愿得一人]
[愿得一人] 2020-11-29 03:04

I want to build a Python function that calculates,

\"alt

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 03:11

    (this answer is meant to be a minor addendum not a complete answer)

    The additional gotcha to unicode identifiers (which @mike-desimone mentions and I discovered quickly when I thought this was a cool thread and switched to a terminal to play with it), is the multiple versions of each glyph are not equivalent, with regards to how you get to each glyph on each platform. For example Σ (aka greek capital letter sigma, aka U+03A3, [can't find a direct mac input method]) is fine, but unfortunately ∑ (aka N-ary Summation, aka U+2211, aka opt/alt-w using Mac OS X) is not a valid identifier.

    >>> Σ = 20
    >>> Σ
    20
    

    but

    >>> ∑ = 20
    File "", line 1
      ∑ = 20
      ^
    SyntaxError: invalid character in identifier
    

    Using Σ specifically (and probably unicode chars in general) as an identifier might generate some very hard to diagnose errors if you have multiple developers on multiple platforms contributing to your code, for example, debug this visually:

    ∑ looks very similar to Σ, depending on the typeface selected

    The two glyphs are easier to differentiate on this page, but depending on the font used, this may not be the case.

    Even the traceback isn't much clearer unless Σ is printed near the ∑

      File "~/Dev/play_python33/identifiers.py", line 12
        print(∑([2, 2, 2, 2, 2]))
                ^
    SyntaxError: invalid character in identifier
    

提交回复
热议问题